]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/hwmon/w83793.c
hwmon/w83793: Ignore disabled temperature channels
[net-next-2.6.git] / drivers / hwmon / w83793.c
CommitLineData
6800c3d0
RM
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 };
ddca933b 120static u8 scale_in_add[] = { 0, 0, 0, 0, 0, 0, 0, 150, 150, 0 };
6800c3d0
RM
121
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;
46bed4df 207 u8 has_temp;
6800c3d0
RM
208 u8 pwm_enable; /* Register value, each Temp has 1 bit */
209 u8 pwm_uptime; /* Register value */
210 u8 pwm_downtime; /* Register value */
211 u8 pwm_default; /* All fan default pwm, next poweron valid */
212 u8 pwm[8][3]; /* Register value */
213 u8 pwm_stop_time[8];
214 u8 temp_cruise[6];
215
216 u8 alarms[5]; /* realtime status registers */
217 u8 beeps[5];
218 u8 beep_enable;
219 u8 tolerance[3]; /* Temp tolerance(Smart Fan I/II) */
220 u8 sf2_pwm[6][7]; /* Smart FanII: Fan duty cycle */
221 u8 sf2_temp[6][7]; /* Smart FanII: Temp level point */
222};
223
224static u8 w83793_read_value(struct i2c_client *client, u16 reg);
225static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
226static int w83793_attach_adapter(struct i2c_adapter *adapter);
227static int w83793_detect(struct i2c_adapter *adapter, int address, int kind);
228static int w83793_detach_client(struct i2c_client *client);
229static void w83793_init_client(struct i2c_client *client);
230static void w83793_update_nonvolatile(struct device *dev);
231static struct w83793_data *w83793_update_device(struct device *dev);
232
233static struct i2c_driver w83793_driver = {
234 .driver = {
235 .name = "w83793",
236 },
237 .attach_adapter = w83793_attach_adapter,
238 .detach_client = w83793_detach_client,
239};
240
241static ssize_t
242show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
243{
244 struct i2c_client *client = to_i2c_client(dev);
245 struct w83793_data *data = i2c_get_clientdata(client);
246
247 return sprintf(buf, "%d\n", data->vrm);
248}
249
250static ssize_t
251show_vid(struct device *dev, struct device_attribute *attr, char *buf)
252{
253 struct w83793_data *data = w83793_update_device(dev);
254 struct sensor_device_attribute_2 *sensor_attr =
255 to_sensor_dev_attr_2(attr);
256 int index = sensor_attr->index;
257
258 return sprintf(buf, "%d\n", vid_from_reg(data->vid[index], data->vrm));
259}
260
261static ssize_t
262store_vrm(struct device *dev, struct device_attribute *attr,
263 const char *buf, size_t count)
264{
265 struct i2c_client *client = to_i2c_client(dev);
266 struct w83793_data *data = i2c_get_clientdata(client);
267
268 data->vrm = simple_strtoul(buf, NULL, 10);
269 return count;
270}
271
272#define ALARM_STATUS 0
273#define BEEP_ENABLE 1
274static ssize_t
275show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
276{
277 struct w83793_data *data = w83793_update_device(dev);
278 struct sensor_device_attribute_2 *sensor_attr =
279 to_sensor_dev_attr_2(attr);
280 int nr = sensor_attr->nr;
281 int index = sensor_attr->index >> 3;
282 int bit = sensor_attr->index & 0x07;
283 u8 val;
284
285 if (ALARM_STATUS == nr) {
286 val = (data->alarms[index] >> (bit)) & 1;
287 } else { /* BEEP_ENABLE */
288 val = (data->beeps[index] >> (bit)) & 1;
289 }
290
291 return sprintf(buf, "%u\n", val);
292}
293
294static ssize_t
295store_beep(struct device *dev, struct device_attribute *attr,
296 const char *buf, size_t count)
297{
298 struct i2c_client *client = to_i2c_client(dev);
299 struct w83793_data *data = i2c_get_clientdata(client);
300 struct sensor_device_attribute_2 *sensor_attr =
301 to_sensor_dev_attr_2(attr);
302 int index = sensor_attr->index >> 3;
303 int shift = sensor_attr->index & 0x07;
304 u8 beep_bit = 1 << shift;
305 u8 val;
306
307 val = simple_strtoul(buf, NULL, 10);
308 if (val != 0 && val != 1)
309 return -EINVAL;
310
311 mutex_lock(&data->update_lock);
312 data->beeps[index] = w83793_read_value(client, W83793_REG_BEEP(index));
313 data->beeps[index] &= ~beep_bit;
314 data->beeps[index] |= val << shift;
315 w83793_write_value(client, W83793_REG_BEEP(index), data->beeps[index]);
316 mutex_unlock(&data->update_lock);
317
318 return count;
319}
320
321static ssize_t
322show_beep_enable(struct device *dev, struct device_attribute *attr, char *buf)
323{
324 struct w83793_data *data = w83793_update_device(dev);
325 return sprintf(buf, "%u\n", (data->beep_enable >> 1) & 0x01);
326}
327
328static ssize_t
329store_beep_enable(struct device *dev, struct device_attribute *attr,
330 const char *buf, size_t count)
331{
332 struct i2c_client *client = to_i2c_client(dev);
333 struct w83793_data *data = i2c_get_clientdata(client);
334 u8 val = simple_strtoul(buf, NULL, 10);
335
336 if (val != 0 && val != 1)
337 return -EINVAL;
338
339 mutex_lock(&data->update_lock);
340 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP)
341 & 0xfd;
342 data->beep_enable |= val << 1;
343 w83793_write_value(client, W83793_REG_OVT_BEEP, data->beep_enable);
344 mutex_unlock(&data->update_lock);
345
346 return count;
347}
348
349/* Write any value to clear chassis alarm */
350static ssize_t
351store_chassis_clear(struct device *dev,
352 struct device_attribute *attr, const char *buf,
353 size_t count)
354{
355 struct i2c_client *client = to_i2c_client(dev);
356 struct w83793_data *data = i2c_get_clientdata(client);
357 u8 val;
358
359 mutex_lock(&data->update_lock);
360 val = w83793_read_value(client, W83793_REG_CLR_CHASSIS);
361 val |= 0x80;
362 w83793_write_value(client, W83793_REG_CLR_CHASSIS, val);
363 mutex_unlock(&data->update_lock);
364 return count;
365}
366
367#define FAN_INPUT 0
368#define FAN_MIN 1
369static ssize_t
370show_fan(struct device *dev, struct device_attribute *attr, char *buf)
371{
372 struct sensor_device_attribute_2 *sensor_attr =
373 to_sensor_dev_attr_2(attr);
374 int nr = sensor_attr->nr;
375 int index = sensor_attr->index;
376 struct w83793_data *data = w83793_update_device(dev);
377 u16 val;
378
379 if (FAN_INPUT == nr) {
380 val = data->fan[index] & 0x0fff;
381 } else {
382 val = data->fan_min[index] & 0x0fff;
383 }
384
385 return sprintf(buf, "%lu\n", FAN_FROM_REG(val));
386}
387
388static ssize_t
389store_fan_min(struct device *dev, struct device_attribute *attr,
390 const char *buf, size_t count)
391{
392 struct sensor_device_attribute_2 *sensor_attr =
393 to_sensor_dev_attr_2(attr);
394 int index = sensor_attr->index;
395 struct i2c_client *client = to_i2c_client(dev);
396 struct w83793_data *data = i2c_get_clientdata(client);
397 u16 val = FAN_TO_REG(simple_strtoul(buf, NULL, 10));
398
399 mutex_lock(&data->update_lock);
400 data->fan_min[index] = val;
401 w83793_write_value(client, W83793_REG_FAN_MIN(index),
402 (val >> 8) & 0xff);
403 w83793_write_value(client, W83793_REG_FAN_MIN(index) + 1, val & 0xff);
404 mutex_unlock(&data->update_lock);
405
406 return count;
407}
408
409#define PWM_DUTY 0
410#define PWM_START 1
411#define PWM_NONSTOP 2
412#define PWM_STOP_TIME 3
413static ssize_t
414show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
415{
416 struct sensor_device_attribute_2 *sensor_attr =
417 to_sensor_dev_attr_2(attr);
418 struct w83793_data *data = w83793_update_device(dev);
419 u16 val;
420 int nr = sensor_attr->nr;
421 int index = sensor_attr->index;
422
423 if (PWM_STOP_TIME == nr)
424 val = TIME_FROM_REG(data->pwm_stop_time[index]);
425 else
426 val = (data->pwm[index][nr] & 0x3f) << 2;
427
428 return sprintf(buf, "%d\n", val);
429}
430
431static ssize_t
432store_pwm(struct device *dev, struct device_attribute *attr,
433 const char *buf, size_t count)
434{
435 struct i2c_client *client = to_i2c_client(dev);
436 struct w83793_data *data = i2c_get_clientdata(client);
437 struct sensor_device_attribute_2 *sensor_attr =
438 to_sensor_dev_attr_2(attr);
439 int nr = sensor_attr->nr;
440 int index = sensor_attr->index;
441 u8 val;
442
443 mutex_lock(&data->update_lock);
444 if (PWM_STOP_TIME == nr) {
445 val = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
446 data->pwm_stop_time[index] = val;
447 w83793_write_value(client, W83793_REG_PWM_STOP_TIME(index),
448 val);
449 } else {
450 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff)
451 >> 2;
452 data->pwm[index][nr] =
453 w83793_read_value(client, W83793_REG_PWM(index, nr)) & 0xc0;
454 data->pwm[index][nr] |= val;
455 w83793_write_value(client, W83793_REG_PWM(index, nr),
456 data->pwm[index][nr]);
457 }
458
459 mutex_unlock(&data->update_lock);
460 return count;
461}
462
463static ssize_t
464show_temp(struct device *dev, struct device_attribute *attr, char *buf)
465{
466 struct sensor_device_attribute_2 *sensor_attr =
467 to_sensor_dev_attr_2(attr);
468 int nr = sensor_attr->nr;
469 int index = sensor_attr->index;
470 struct w83793_data *data = w83793_update_device(dev);
471 long temp = TEMP_FROM_REG(data->temp[index][nr]);
472
473 if (TEMP_READ == nr && index < 4) { /* Only TD1-TD4 have low bits */
474 int low = ((data->temp_low_bits >> (index * 2)) & 0x03) * 250;
475 temp += temp > 0 ? low : -low;
476 }
477 return sprintf(buf, "%ld\n", temp);
478}
479
480static ssize_t
481store_temp(struct device *dev, struct device_attribute *attr,
482 const char *buf, size_t count)
483{
484 struct sensor_device_attribute_2 *sensor_attr =
485 to_sensor_dev_attr_2(attr);
486 int nr = sensor_attr->nr;
487 int index = sensor_attr->index;
488 struct i2c_client *client = to_i2c_client(dev);
489 struct w83793_data *data = i2c_get_clientdata(client);
490 long tmp = simple_strtol(buf, NULL, 10);
491
492 mutex_lock(&data->update_lock);
493 data->temp[index][nr] = TEMP_TO_REG(tmp, -128, 127);
494 w83793_write_value(client, W83793_REG_TEMP[index][nr],
495 data->temp[index][nr]);
496 mutex_unlock(&data->update_lock);
497 return count;
498}
499
500/*
501 TD1-TD4
502 each has 4 mode:(2 bits)
503 0: Stop monitor
504 1: Use internal temp sensor(default)
ddca933b 505 2: Reserved
6800c3d0
RM
506 3: Use sensor in Intel CPU and get result by PECI
507
508 TR1-TR2
509 each has 2 mode:(1 bit)
510 0: Disable temp sensor monitor
511 1: To enable temp sensors monitor
512*/
513
ddca933b
GJ
514/* 0 disable, 6 PECI */
515static u8 TO_TEMP_MODE[] = { 0, 0, 0, 6 };
6800c3d0
RM
516
517static ssize_t
518show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
519{
520 struct w83793_data *data = w83793_update_device(dev);
521 struct sensor_device_attribute_2 *sensor_attr =
522 to_sensor_dev_attr_2(attr);
523 int index = sensor_attr->index;
524 u8 mask = (index < 4) ? 0x03 : 0x01;
525 u8 shift = (index < 4) ? (2 * index) : (index - 4);
526 u8 tmp;
527 index = (index < 4) ? 0 : 1;
528
529 tmp = (data->temp_mode[index] >> shift) & mask;
530
531 /* for the internal sensor, found out if diode or thermistor */
532 if (tmp == 1) {
533 tmp = index == 0 ? 3 : 4;
534 } else {
535 tmp = TO_TEMP_MODE[tmp];
536 }
537
538 return sprintf(buf, "%d\n", tmp);
539}
540
541static ssize_t
542store_temp_mode(struct device *dev, struct device_attribute *attr,
543 const char *buf, size_t count)
544{
545 struct i2c_client *client = to_i2c_client(dev);
546 struct w83793_data *data = i2c_get_clientdata(client);
547 struct sensor_device_attribute_2 *sensor_attr =
548 to_sensor_dev_attr_2(attr);
549 int index = sensor_attr->index;
550 u8 mask = (index < 4) ? 0x03 : 0x01;
551 u8 shift = (index < 4) ? (2 * index) : (index - 4);
552 u8 val = simple_strtoul(buf, NULL, 10);
553
554 /* transform the sysfs interface values into table above */
ddca933b 555 if ((val == 6) && (index < 4)) {
6800c3d0
RM
556 val -= 3;
557 } else if ((val == 3 && index < 4)
46bed4df 558 || (val == 4 && index >= 4)) {
6800c3d0
RM
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 }
ddca933b
GJ
843 /* 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);
6800c3d0
RM
846}
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) {
ddca933b
GJ
865 /* 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 }
6800c3d0
RM
869 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),
6800c3d0
RM
989 SENSOR_ATTR_FAN(1),
990 SENSOR_ATTR_FAN(2),
991 SENSOR_ATTR_FAN(3),
992 SENSOR_ATTR_FAN(4),
993 SENSOR_ATTR_FAN(5),
994 SENSOR_ATTR_PWM(1),
995 SENSOR_ATTR_PWM(2),
996 SENSOR_ATTR_PWM(3),
997};
998
46bed4df
GJ
999static struct sensor_device_attribute_2 w83793_temp[] = {
1000 SENSOR_ATTR_TEMP(1),
1001 SENSOR_ATTR_TEMP(2),
1002 SENSOR_ATTR_TEMP(3),
1003 SENSOR_ATTR_TEMP(4),
1004 SENSOR_ATTR_TEMP(5),
1005 SENSOR_ATTR_TEMP(6),
1006};
1007
6800c3d0
RM
1008/* Fan6-Fan12 */
1009static struct sensor_device_attribute_2 w83793_left_fan[] = {
1010 SENSOR_ATTR_FAN(6),
1011 SENSOR_ATTR_FAN(7),
1012 SENSOR_ATTR_FAN(8),
1013 SENSOR_ATTR_FAN(9),
1014 SENSOR_ATTR_FAN(10),
1015 SENSOR_ATTR_FAN(11),
1016 SENSOR_ATTR_FAN(12),
1017};
1018
1019/* Pwm4-Pwm8 */
1020static struct sensor_device_attribute_2 w83793_left_pwm[] = {
1021 SENSOR_ATTR_PWM(4),
1022 SENSOR_ATTR_PWM(5),
1023 SENSOR_ATTR_PWM(6),
1024 SENSOR_ATTR_PWM(7),
1025 SENSOR_ATTR_PWM(8),
1026};
1027
1028static struct sensor_device_attribute_2 sda_single_files[] = {
1029 SENSOR_ATTR_2(cpu0_vid, S_IRUGO, show_vid, NULL, NOT_USED, 0),
1030 SENSOR_ATTR_2(cpu1_vid, S_IRUGO, show_vid, NULL, NOT_USED, 1),
1031 SENSOR_ATTR_2(vrm, S_IWUSR | S_IRUGO, show_vrm, store_vrm,
1032 NOT_USED, NOT_USED),
1033 SENSOR_ATTR_2(chassis, S_IWUSR | S_IRUGO, show_alarm_beep,
1034 store_chassis_clear, ALARM_STATUS, 30),
1035 SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_beep_enable,
1036 store_beep_enable, NOT_USED, NOT_USED),
1037 SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1038 store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1039 SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1040 store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1041 SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1042 store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
1043 SENSOR_ATTR_2(temp_critical, S_IWUSR | S_IRUGO, show_sf_setup,
1044 store_sf_setup, SETUP_TEMP_CRITICAL, NOT_USED),
1045};
1046
1047static void w83793_init_client(struct i2c_client *client)
1048{
1049 if (reset) {
1050 w83793_write_value(client, W83793_REG_CONFIG, 0x80);
1051 }
1052
1053 /* Start monitoring */
1054 w83793_write_value(client, W83793_REG_CONFIG,
1055 w83793_read_value(client, W83793_REG_CONFIG) | 0x01);
1056
1057}
1058
1059static int w83793_attach_adapter(struct i2c_adapter *adapter)
1060{
1061 if (!(adapter->class & I2C_CLASS_HWMON))
1062 return 0;
1063 return i2c_probe(adapter, &addr_data, w83793_detect);
1064}
1065
1066static int w83793_detach_client(struct i2c_client *client)
1067{
1068 struct w83793_data *data = i2c_get_clientdata(client);
1069 struct device *dev = &client->dev;
1070 int err, i;
1071
1072 /* main client */
1073 if (data) {
1074 hwmon_device_unregister(data->class_dev);
1075
1076 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1077 device_remove_file(dev,
1078 &w83793_sensor_attr_2[i].dev_attr);
1079
1080 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1081 device_remove_file(dev, &sda_single_files[i].dev_attr);
1082
1083 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1084 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1085
1086 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1087 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
46bed4df
GJ
1088
1089 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1090 device_remove_file(dev, &w83793_temp[i].dev_attr);
6800c3d0
RM
1091 }
1092
1093 if ((err = i2c_detach_client(client)))
1094 return err;
1095
1096 /* main client */
1097 if (data)
1098 kfree(data);
1099 /* subclient */
1100 else
1101 kfree(client);
1102
1103 return 0;
1104}
1105
1106static int
1107w83793_create_subclient(struct i2c_adapter *adapter,
1108 struct i2c_client *client, int addr,
1109 struct i2c_client **sub_cli)
1110{
1111 int err = 0;
1112 struct i2c_client *sub_client;
1113
1114 (*sub_cli) = sub_client =
1115 kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1116 if (!(sub_client)) {
1117 return -ENOMEM;
1118 }
1119 sub_client->addr = 0x48 + addr;
1120 i2c_set_clientdata(sub_client, NULL);
1121 sub_client->adapter = adapter;
1122 sub_client->driver = &w83793_driver;
1123 strlcpy(sub_client->name, "w83793 subclient", I2C_NAME_SIZE);
1124 if ((err = i2c_attach_client(sub_client))) {
1125 dev_err(&client->dev, "subclient registration "
1126 "at address 0x%x failed\n", sub_client->addr);
1127 kfree(sub_client);
1128 }
1129 return err;
1130}
1131
1132static int
1133w83793_detect_subclients(struct i2c_adapter *adapter, int address,
1134 int kind, struct i2c_client *client)
1135{
1136 int i, id, err;
1137 u8 tmp;
1138 struct w83793_data *data = i2c_get_clientdata(client);
1139
1140 id = i2c_adapter_id(adapter);
1141 if (force_subclients[0] == id && force_subclients[1] == address) {
1142 for (i = 2; i <= 3; i++) {
1143 if (force_subclients[i] < 0x48
1144 || force_subclients[i] > 0x4f) {
1145 dev_err(&client->dev,
1146 "invalid subclient "
1147 "address %d; must be 0x48-0x4f\n",
1148 force_subclients[i]);
1149 err = -EINVAL;
1150 goto ERROR_SC_0;
1151 }
1152 }
1153 w83793_write_value(client, W83793_REG_I2C_SUBADDR,
1154 (force_subclients[2] & 0x07) |
1155 ((force_subclients[3] & 0x07) << 4));
1156 }
1157
1158 tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
1159 if (!(tmp & 0x08)) {
1160 err =
1161 w83793_create_subclient(adapter, client, tmp & 0x7,
1162 &data->lm75[0]);
1163 if (err < 0)
1164 goto ERROR_SC_0;
1165 }
1166 if (!(tmp & 0x80)) {
1167 if ((data->lm75[0] != NULL)
1168 && ((tmp & 0x7) == ((tmp >> 4) & 0x7))) {
1169 dev_err(&client->dev,
1170 "duplicate addresses 0x%x, "
1171 "use force_subclients\n", data->lm75[0]->addr);
1172 err = -ENODEV;
1173 goto ERROR_SC_1;
1174 }
1175 err = w83793_create_subclient(adapter, client,
1176 (tmp >> 4) & 0x7, &data->lm75[1]);
1177 if (err < 0)
1178 goto ERROR_SC_1;
1179 }
1180
1181 return 0;
1182
1183 /* Undo inits in case of errors */
1184
1185ERROR_SC_1:
1186 if (data->lm75[0] != NULL) {
1187 i2c_detach_client(data->lm75[0]);
1188 kfree(data->lm75[0]);
1189 }
1190ERROR_SC_0:
1191 return err;
1192}
1193
1194static int w83793_detect(struct i2c_adapter *adapter, int address, int kind)
1195{
1196 int i;
1197 u8 tmp, val;
1198 struct i2c_client *client;
1199 struct device *dev;
1200 struct w83793_data *data;
1201 int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
1202 int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
46bed4df 1203 int files_temp = ARRAY_SIZE(w83793_temp) / 6;
6800c3d0
RM
1204 int err = 0;
1205
1206 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1207 goto exit;
1208 }
1209
1210 /* OK. For now, we presume we have a valid client. We now create the
1211 client structure, even though we cannot fill it completely yet.
1212 But it allows us to access w83793_{read,write}_value. */
1213
1214 if (!(data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL))) {
1215 err = -ENOMEM;
1216 goto exit;
1217 }
1218
1219 client = &data->client;
1220 dev = &client->dev;
1221 i2c_set_clientdata(client, data);
1222 client->addr = address;
1223 client->adapter = adapter;
1224 client->driver = &w83793_driver;
1225
1226 data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
1227
1228 /* Now, we do the remaining detection. */
1229 if (kind < 0) {
1230 tmp = data->bank & 0x80 ? 0x5c : 0xa3;
1231 /* Check Winbond vendor ID */
1232 if (tmp != i2c_smbus_read_byte_data(client,
1233 W83793_REG_VENDORID)) {
1234 pr_debug("w83793: Detection failed at check "
1235 "vendor id\n");
1236 err = -ENODEV;
1237 goto free_mem;
1238 }
1239
1240 /* If Winbond chip, address of chip and W83793_REG_I2C_ADDR
1241 should match */
1242 if ((data->bank & 0x07) == 0
1243 && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
1244 (address << 1)) {
1245 pr_debug("w83793: Detection failed at check "
1246 "i2c addr\n");
1247 err = -ENODEV;
1248 goto free_mem;
1249 }
1250
1251 }
1252
1253 /* We have either had a force parameter, or we have already detected the
1254 Winbond. Determine the chip type now */
1255
1256 if (kind <= 0) {
1257 if (0x7b == w83793_read_value(client, W83793_REG_CHIPID)) {
1258 kind = w83793;
1259 } else {
1260 if (kind == 0)
1261 dev_warn(&adapter->dev, "w83793: Ignoring "
1262 "'force' parameter for unknown chip "
1263 "at address 0x%02x\n", address);
1264 err = -ENODEV;
1265 goto free_mem;
1266 }
1267 }
1268
1269 /* Fill in the remaining client fields and put into the global list */
1270 strlcpy(client->name, "w83793", I2C_NAME_SIZE);
1271
1272 mutex_init(&data->update_lock);
1273
1274 /* Tell the I2C layer a new client has arrived */
1275 if ((err = i2c_attach_client(client)))
1276 goto free_mem;
1277
1278 if ((err = w83793_detect_subclients(adapter, address, kind, client)))
1279 goto detach_client;
1280
1281 /* Initialize the chip */
1282 w83793_init_client(client);
1283
1284 data->vrm = vid_which_vrm();
1285 /*
1286 Only fan 1-5 has their own input pins,
1287 Pwm 1-3 has their own pins
1288 */
1289 data->has_fan = 0x1f;
1290 data->has_pwm = 0x07;
1291 tmp = w83793_read_value(client, W83793_REG_MFC);
1292 val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
1293
1294 /* check the function of pins 49-56 */
1295 if (!(tmp & 0x80)) {
1296 data->has_pwm |= 0x18; /* pwm 4,5 */
1297 if (val & 0x01) { /* fan 6 */
1298 data->has_fan |= 0x20;
1299 data->has_pwm |= 0x20;
1300 }
1301 if (val & 0x02) { /* fan 7 */
1302 data->has_fan |= 0x40;
1303 data->has_pwm |= 0x40;
1304 }
1305 if (!(tmp & 0x40) && (val & 0x04)) { /* fan 8 */
1306 data->has_fan |= 0x80;
1307 data->has_pwm |= 0x80;
1308 }
1309 }
1310
1311 if (0x08 == (tmp & 0x0c)) {
1312 if (val & 0x08) /* fan 9 */
1313 data->has_fan |= 0x100;
1314 if (val & 0x10) /* fan 10 */
1315 data->has_fan |= 0x200;
1316 }
1317
1318 if (0x20 == (tmp & 0x30)) {
1319 if (val & 0x20) /* fan 11 */
1320 data->has_fan |= 0x400;
1321 if (val & 0x40) /* fan 12 */
1322 data->has_fan |= 0x800;
1323 }
1324
1325 if ((tmp & 0x01) && (val & 0x04)) { /* fan 8, second location */
1326 data->has_fan |= 0x80;
1327 data->has_pwm |= 0x80;
1328 }
1329
46bed4df
GJ
1330 /* check the temp1-6 mode, ignore former AMDSI selected inputs */
1331 tmp = w83793_read_value(client,W83793_REG_TEMP_MODE[0]);
1332 if (tmp & 0x01)
1333 data->has_temp |= 0x01;
1334 if (tmp & 0x04)
1335 data->has_temp |= 0x02;
1336 if (tmp & 0x10)
1337 data->has_temp |= 0x04;
1338 if (tmp & 0x40)
1339 data->has_temp |= 0x08;
1340
1341 tmp = w83793_read_value(client,W83793_REG_TEMP_MODE[1]);
1342 if (tmp & 0x01)
1343 data->has_temp |= 0x10;
1344 if (tmp & 0x02)
1345 data->has_temp |= 0x20;
1346
6800c3d0
RM
1347 /* Register sysfs hooks */
1348 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
1349 err = device_create_file(dev,
1350 &w83793_sensor_attr_2[i].dev_attr);
1351 if (err)
1352 goto exit_remove;
1353 }
1354
1355 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
1356 err = device_create_file(dev, &sda_single_files[i].dev_attr);
1357 if (err)
1358 goto exit_remove;
1359
1360 }
1361
46bed4df
GJ
1362 for (i = 0; i < 6; i++) {
1363 int j;
1364 if (!(data->has_temp & (1 << i)))
1365 continue;
1366 for (j = 0; j < files_temp; j++) {
1367 err = device_create_file(dev,
1368 &w83793_temp[(i) * files_temp
1369 + j].dev_attr);
1370 if (err)
1371 goto exit_remove;
1372 }
1373 }
1374
6800c3d0
RM
1375 for (i = 5; i < 12; i++) {
1376 int j;
1377 if (!(data->has_fan & (1 << i)))
1378 continue;
1379 for (j = 0; j < files_fan; j++) {
1380 err = device_create_file(dev,
1381 &w83793_left_fan[(i - 5) * files_fan
1382 + j].dev_attr);
1383 if (err)
1384 goto exit_remove;
1385 }
1386 }
1387
1388 for (i = 3; i < 8; i++) {
1389 int j;
1390 if (!(data->has_pwm & (1 << i)))
1391 continue;
1392 for (j = 0; j < files_pwm; j++) {
1393 err = device_create_file(dev,
1394 &w83793_left_pwm[(i - 3) * files_pwm
1395 + j].dev_attr);
1396 if (err)
1397 goto exit_remove;
1398 }
1399 }
1400
1401 data->class_dev = hwmon_device_register(dev);
1402 if (IS_ERR(data->class_dev)) {
1403 err = PTR_ERR(data->class_dev);
1404 goto exit_remove;
1405 }
1406
1407 return 0;
1408
1409 /* Unregister sysfs hooks */
1410
1411exit_remove:
1412 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1413 device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
1414
1415 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1416 device_remove_file(dev, &sda_single_files[i].dev_attr);
1417
1418 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1419 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1420
1421 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1422 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1423
46bed4df
GJ
1424 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1425 device_remove_file(dev, &w83793_temp[i].dev_attr);
1426
6800c3d0
RM
1427 if (data->lm75[0] != NULL) {
1428 i2c_detach_client(data->lm75[0]);
1429 kfree(data->lm75[0]);
1430 }
1431 if (data->lm75[1] != NULL) {
1432 i2c_detach_client(data->lm75[1]);
1433 kfree(data->lm75[1]);
1434 }
1435detach_client:
1436 i2c_detach_client(client);
1437free_mem:
1438 kfree(data);
1439exit:
1440 return err;
1441}
1442
1443static void w83793_update_nonvolatile(struct device *dev)
1444{
1445 struct i2c_client *client = to_i2c_client(dev);
1446 struct w83793_data *data = i2c_get_clientdata(client);
1447 int i, j;
1448 /*
1449 They are somewhat "stable" registers, and to update them everytime
1450 takes so much time, it's just not worthy. Update them in a long
1451 interval to avoid exception.
1452 */
1453 if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
1454 || !data->valid))
1455 return;
1456 /* update voltage limits */
1457 for (i = 1; i < 3; i++) {
1458 for (j = 0; j < ARRAY_SIZE(data->in); j++) {
1459 data->in[j][i] =
1460 w83793_read_value(client, W83793_REG_IN[j][i]);
1461 }
1462 data->in_low_bits[i] =
1463 w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
1464 }
1465
1466 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1467 /* Update the Fan measured value and limits */
1468 if (!(data->has_fan & (1 << i))) {
1469 continue;
1470 }
1471 data->fan_min[i] =
1472 w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
1473 data->fan_min[i] |=
1474 w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
1475 }
1476
1477 for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
46bed4df
GJ
1478 if (!(data->has_temp & (1 << i)))
1479 continue;
6800c3d0
RM
1480 data->temp_fan_map[i] =
1481 w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
1482 for (j = 1; j < 5; j++) {
1483 data->temp[i][j] =
1484 w83793_read_value(client, W83793_REG_TEMP[i][j]);
1485 }
1486 data->temp_cruise[i] =
1487 w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
1488 for (j = 0; j < 7; j++) {
1489 data->sf2_pwm[i][j] =
1490 w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
1491 data->sf2_temp[i][j] =
1492 w83793_read_value(client,
1493 W83793_REG_SF2_TEMP(i, j));
1494 }
1495 }
1496
1497 for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
1498 data->temp_mode[i] =
1499 w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
1500
1501 for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
1502 data->tolerance[i] =
1503 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
1504 }
1505
1506 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1507 if (!(data->has_pwm & (1 << i)))
1508 continue;
1509 data->pwm[i][PWM_NONSTOP] =
1510 w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
1511 data->pwm[i][PWM_START] =
1512 w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
1513 data->pwm_stop_time[i] =
1514 w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
1515 }
1516
1517 data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
1518 data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
1519 data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
1520 data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
1521 data->temp_critical =
1522 w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
1523 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
1524
1525 for (i = 0; i < ARRAY_SIZE(data->beeps); i++) {
1526 data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
1527 }
1528
1529 data->last_nonvolatile = jiffies;
1530}
1531
1532static struct w83793_data *w83793_update_device(struct device *dev)
1533{
1534 struct i2c_client *client = to_i2c_client(dev);
1535 struct w83793_data *data = i2c_get_clientdata(client);
1536 int i;
1537
1538 mutex_lock(&data->update_lock);
1539
1540 if (!(time_after(jiffies, data->last_updated + HZ * 2)
1541 || !data->valid))
1542 goto END;
1543
1544 /* Update the voltages measured value and limits */
1545 for (i = 0; i < ARRAY_SIZE(data->in); i++)
1546 data->in[i][IN_READ] =
1547 w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
1548
1549 data->in_low_bits[IN_READ] =
1550 w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
1551
1552 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
1553 if (!(data->has_fan & (1 << i))) {
1554 continue;
1555 }
1556 data->fan[i] =
1557 w83793_read_value(client, W83793_REG_FAN(i)) << 8;
1558 data->fan[i] |=
1559 w83793_read_value(client, W83793_REG_FAN(i) + 1);
1560 }
1561
46bed4df
GJ
1562 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
1563 if (!(data->has_temp & (1 << i)))
1564 continue;
6800c3d0
RM
1565 data->temp[i][TEMP_READ] =
1566 w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
46bed4df 1567 }
6800c3d0
RM
1568
1569 data->temp_low_bits =
1570 w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
1571
1572 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1573 if (data->has_pwm & (1 << i))
1574 data->pwm[i][PWM_DUTY] =
1575 w83793_read_value(client,
1576 W83793_REG_PWM(i, PWM_DUTY));
1577 }
1578
1579 for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
1580 data->alarms[i] =
1581 w83793_read_value(client, W83793_REG_ALARM(i));
1582 data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
1583 data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
1584 w83793_update_nonvolatile(dev);
1585 data->last_updated = jiffies;
1586 data->valid = 1;
1587
1588END:
1589 mutex_unlock(&data->update_lock);
1590 return data;
1591}
1592
1593/* Ignore the possibility that somebody change bank outside the driver
1594 Must be called with data->update_lock held, except during initialization */
1595static u8 w83793_read_value(struct i2c_client *client, u16 reg)
1596{
1597 struct w83793_data *data = i2c_get_clientdata(client);
1598 u8 res = 0xff;
1599 u8 new_bank = reg >> 8;
1600
1601 new_bank |= data->bank & 0xfc;
1602 if (data->bank != new_bank) {
1603 if (i2c_smbus_write_byte_data
1604 (client, W83793_REG_BANKSEL, new_bank) >= 0)
1605 data->bank = new_bank;
1606 else {
1607 dev_err(&client->dev,
1608 "set bank to %d failed, fall back "
1609 "to bank %d, read reg 0x%x error\n",
1610 new_bank, data->bank, reg);
1611 res = 0x0; /* read 0x0 from the chip */
1612 goto END;
1613 }
1614 }
1615 res = i2c_smbus_read_byte_data(client, reg & 0xff);
1616END:
1617 return res;
1618}
1619
1620/* Must be called with data->update_lock held, except during initialization */
1621static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
1622{
1623 struct w83793_data *data = i2c_get_clientdata(client);
1624 int res;
1625 u8 new_bank = reg >> 8;
1626
1627 new_bank |= data->bank & 0xfc;
1628 if (data->bank != new_bank) {
1629 if ((res = i2c_smbus_write_byte_data
1630 (client, W83793_REG_BANKSEL, new_bank)) >= 0)
1631 data->bank = new_bank;
1632 else {
1633 dev_err(&client->dev,
1634 "set bank to %d failed, fall back "
1635 "to bank %d, write reg 0x%x error\n",
1636 new_bank, data->bank, reg);
1637 goto END;
1638 }
1639 }
1640
1641 res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
1642END:
1643 return res;
1644}
1645
1646static int __init sensors_w83793_init(void)
1647{
1648 return i2c_add_driver(&w83793_driver);
1649}
1650
1651static void __exit sensors_w83793_exit(void)
1652{
1653 i2c_del_driver(&w83793_driver);
1654}
1655
1656MODULE_AUTHOR("Yuan Mu");
1657MODULE_DESCRIPTION("w83793 driver");
1658MODULE_LICENSE("GPL");
1659
1660module_init(sensors_w83793_init);
1661module_exit(sensors_w83793_exit);