2 w83l786ng.c - Linux kernel driver for hardware monitoring
3 Copyright (c) 2007 Kevin Lo <kevlo@kevlo.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation - version 2.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Supports following chips:
23 Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
24 w83l786ng 3 2 2 2 0x7b 0x5ca3 yes no
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/i2c.h>
31 #include <linux/hwmon.h>
32 #include <linux/hwmon-vid.h>
33 #include <linux/hwmon-sysfs.h>
34 #include <linux/err.h>
35 #include <linux/mutex.h>
37 /* Addresses to scan */
38 <<<<<<< HEAD
:drivers
/hwmon
/w83l786ng
.c
39 static unsigned short normal_i2c
[] = { 0x2e, 0x2f, I2C_CLIENT_END
};
41 static const unsigned short normal_i2c
[] = { 0x2e, 0x2f, I2C_CLIENT_END
};
42 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/w83l786ng
.c
44 /* Insmod parameters */
45 I2C_CLIENT_INSMOD_1(w83l786ng
);
48 module_param(reset
, bool, 0);
49 MODULE_PARM_DESC(reset
, "Set to 1 to reset chip, not recommended");
51 #define W83L786NG_REG_IN_MIN(nr) (0x2C + (nr) * 2)
52 #define W83L786NG_REG_IN_MAX(nr) (0x2B + (nr) * 2)
53 #define W83L786NG_REG_IN(nr) ((nr) + 0x20)
55 #define W83L786NG_REG_FAN(nr) ((nr) + 0x28)
56 #define W83L786NG_REG_FAN_MIN(nr) ((nr) + 0x3B)
58 #define W83L786NG_REG_CONFIG 0x40
59 #define W83L786NG_REG_ALARM1 0x41
60 #define W83L786NG_REG_ALARM2 0x42
61 #define W83L786NG_REG_GPIO_EN 0x47
62 #define W83L786NG_REG_MAN_ID2 0x4C
63 #define W83L786NG_REG_MAN_ID1 0x4D
64 #define W83L786NG_REG_CHIP_ID 0x4E
66 #define W83L786NG_REG_DIODE 0x53
67 #define W83L786NG_REG_FAN_DIV 0x54
68 #define W83L786NG_REG_FAN_CFG 0x80
70 #define W83L786NG_REG_TOLERANCE 0x8D
72 static const u8 W83L786NG_REG_TEMP
[2][3] = {
73 { 0x25, /* TEMP 0 in DataSheet */
74 0x35, /* TEMP 0 Over in DataSheet */
75 0x36 }, /* TEMP 0 Hyst in DataSheet */
76 { 0x26, /* TEMP 1 in DataSheet */
77 0x37, /* TEMP 1 Over in DataSheet */
78 0x38 } /* TEMP 1 Hyst in DataSheet */
81 static const u8 W83L786NG_PWM_MODE_SHIFT
[] = {6, 7};
82 static const u8 W83L786NG_PWM_ENABLE_SHIFT
[] = {2, 4};
84 /* FAN Duty Cycle, be used to control */
85 static const u8 W83L786NG_REG_PWM
[] = {0x81, 0x87};
89 FAN_TO_REG(long rpm
, int div
)
93 rpm
= SENSORS_LIMIT(rpm
, 1, 1000000);
94 return SENSORS_LIMIT((1350000 + rpm
* div
/ 2) / (rpm
* div
), 1, 254);
97 #define FAN_FROM_REG(val,div) ((val) == 0 ? -1 : \
99 1350000 / ((val) * (div))))
102 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (val)+0x100*1000 \
103 : (val)) / 1000, 0, 0xff))
104 #define TEMP_FROM_REG(val) (((val) & 0x80 ? (val)-0x100 : (val)) * 1000)
106 /* The analog voltage inputs have 8mV LSB. Since the sysfs output is
107 in mV as would be measured on the chip input pin, need to just
108 multiply/divide by 8 to translate from/to register values. */
109 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 4) / 8), 0, 255))
110 #define IN_FROM_REG(val) ((val) * 8)
112 #define DIV_FROM_REG(val) (1 << (val))
118 val
= SENSORS_LIMIT(val
, 1, 128) >> 1;
119 for (i
= 0; i
< 7; i
++) {
127 struct w83l786ng_data
{
128 struct i2c_client client
;
129 struct device
*hwmon_dev
;
130 struct mutex update_lock
;
131 char valid
; /* !=0 if following fields are valid */
132 unsigned long last_updated
; /* In jiffies */
133 unsigned long last_nonvolatile
; /* In jiffies, last time we update the
134 nonvolatile registers */
145 u8 pwm_mode
[2]; /* 0->DC variable voltage
146 1->PWM variable duty cycle */
148 u8 pwm_enable
[2]; /* 1->manual
149 2->thermal cruise (also called SmartFan I) */
153 static int w83l786ng_attach_adapter(struct i2c_adapter
*adapter
);
154 static int w83l786ng_detect(struct i2c_adapter
*adapter
, int address
, int kind
);
155 static int w83l786ng_detach_client(struct i2c_client
*client
);
156 static void w83l786ng_init_client(struct i2c_client
*client
);
157 static struct w83l786ng_data
*w83l786ng_update_device(struct device
*dev
);
159 static struct i2c_driver w83l786ng_driver
= {
163 .attach_adapter
= w83l786ng_attach_adapter
,
164 .detach_client
= w83l786ng_detach_client
,
168 w83l786ng_read_value(struct i2c_client
*client
, u8 reg
)
170 return i2c_smbus_read_byte_data(client
, reg
);
174 w83l786ng_write_value(struct i2c_client
*client
, u8 reg
, u8 value
)
176 return i2c_smbus_write_byte_data(client
, reg
, value
);
179 /* following are the sysfs callback functions */
180 #define show_in_reg(reg) \
182 show_##reg(struct device *dev, struct device_attribute *attr, \
185 int nr = to_sensor_dev_attr(attr)->index; \
186 struct w83l786ng_data *data = w83l786ng_update_device(dev); \
187 return sprintf(buf,"%d\n", IN_FROM_REG(data->reg[nr])); \
194 #define store_in_reg(REG, reg) \
196 store_in_##reg (struct device *dev, struct device_attribute *attr, \
197 const char *buf, size_t count) \
199 int nr = to_sensor_dev_attr(attr)->index; \
200 struct i2c_client *client = to_i2c_client(dev); \
201 struct w83l786ng_data *data = i2c_get_clientdata(client); \
202 unsigned long val = simple_strtoul(buf, NULL, 10); \
203 mutex_lock(&data->update_lock); \
204 data->in_##reg[nr] = IN_TO_REG(val); \
205 w83l786ng_write_value(client, W83L786NG_REG_IN_##REG(nr), \
206 data->in_##reg[nr]); \
207 mutex_unlock(&data->update_lock); \
211 store_in_reg(MIN
, min
)
212 store_in_reg(MAX
, max
)
214 static struct sensor_device_attribute sda_in_input
[] = {
215 SENSOR_ATTR(in0_input
, S_IRUGO
, show_in
, NULL
, 0),
216 SENSOR_ATTR(in1_input
, S_IRUGO
, show_in
, NULL
, 1),
217 SENSOR_ATTR(in2_input
, S_IRUGO
, show_in
, NULL
, 2),
220 static struct sensor_device_attribute sda_in_min
[] = {
221 SENSOR_ATTR(in0_min
, S_IWUSR
| S_IRUGO
, show_in_min
, store_in_min
, 0),
222 SENSOR_ATTR(in1_min
, S_IWUSR
| S_IRUGO
, show_in_min
, store_in_min
, 1),
223 SENSOR_ATTR(in2_min
, S_IWUSR
| S_IRUGO
, show_in_min
, store_in_min
, 2),
226 static struct sensor_device_attribute sda_in_max
[] = {
227 SENSOR_ATTR(in0_max
, S_IWUSR
| S_IRUGO
, show_in_max
, store_in_max
, 0),
228 SENSOR_ATTR(in1_max
, S_IWUSR
| S_IRUGO
, show_in_max
, store_in_max
, 1),
229 SENSOR_ATTR(in2_max
, S_IWUSR
| S_IRUGO
, show_in_max
, store_in_max
, 2),
232 #define show_fan_reg(reg) \
233 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
236 int nr = to_sensor_dev_attr(attr)->index; \
237 struct w83l786ng_data *data = w83l786ng_update_device(dev); \
238 return sprintf(buf,"%d\n", \
239 FAN_FROM_REG(data->fan[nr], DIV_FROM_REG(data->fan_div[nr]))); \
243 show_fan_reg(fan_min
);
246 store_fan_min(struct device
*dev
, struct device_attribute
*attr
,
247 const char *buf
, size_t count
)
249 int nr
= to_sensor_dev_attr(attr
)->index
;
250 struct i2c_client
*client
= to_i2c_client(dev
);
251 struct w83l786ng_data
*data
= i2c_get_clientdata(client
);
254 val
= simple_strtoul(buf
, NULL
, 10);
255 mutex_lock(&data
->update_lock
);
256 data
->fan_min
[nr
] = FAN_TO_REG(val
, DIV_FROM_REG(data
->fan_div
[nr
]));
257 w83l786ng_write_value(client
, W83L786NG_REG_FAN_MIN(nr
),
259 mutex_unlock(&data
->update_lock
);
265 show_fan_div(struct device
*dev
, struct device_attribute
*attr
,
268 int nr
= to_sensor_dev_attr(attr
)->index
;
269 struct w83l786ng_data
*data
= w83l786ng_update_device(dev
);
270 return sprintf(buf
, "%u\n", DIV_FROM_REG(data
->fan_div
[nr
]));
273 /* Note: we save and restore the fan minimum here, because its value is
274 determined in part by the fan divisor. This follows the principle of
275 least surprise; the user doesn't expect the fan minimum to change just
276 because the divisor changed. */
278 store_fan_div(struct device
*dev
, struct device_attribute
*attr
,
279 const char *buf
, size_t count
)
281 int nr
= to_sensor_dev_attr(attr
)->index
;
282 struct i2c_client
*client
= to_i2c_client(dev
);
283 struct w83l786ng_data
*data
= i2c_get_clientdata(client
);
292 mutex_lock(&data
->update_lock
);
293 min
= FAN_FROM_REG(data
->fan_min
[nr
], DIV_FROM_REG(data
->fan_div
[nr
]));
295 data
->fan_div
[nr
] = DIV_TO_REG(simple_strtoul(buf
, NULL
, 10));
308 fan_div_reg
= w83l786ng_read_value(client
, W83L786NG_REG_FAN_DIV
)
311 tmp_fan_div
= (data
->fan_div
[nr
] << new_shift
) & ~keep_mask
;
313 w83l786ng_write_value(client
, W83L786NG_REG_FAN_DIV
,
314 fan_div_reg
| tmp_fan_div
);
316 /* Restore fan_min */
317 data
->fan_min
[nr
] = FAN_TO_REG(min
, DIV_FROM_REG(data
->fan_div
[nr
]));
318 w83l786ng_write_value(client
, W83L786NG_REG_FAN_MIN(nr
),
320 mutex_unlock(&data
->update_lock
);
325 static struct sensor_device_attribute sda_fan_input
[] = {
326 SENSOR_ATTR(fan1_input
, S_IRUGO
, show_fan
, NULL
, 0),
327 SENSOR_ATTR(fan2_input
, S_IRUGO
, show_fan
, NULL
, 1),
330 static struct sensor_device_attribute sda_fan_min
[] = {
331 SENSOR_ATTR(fan1_min
, S_IWUSR
| S_IRUGO
, show_fan_min
,
333 SENSOR_ATTR(fan2_min
, S_IWUSR
| S_IRUGO
, show_fan_min
,
337 static struct sensor_device_attribute sda_fan_div
[] = {
338 SENSOR_ATTR(fan1_div
, S_IWUSR
| S_IRUGO
, show_fan_div
,
340 SENSOR_ATTR(fan2_div
, S_IWUSR
| S_IRUGO
, show_fan_div
,
345 /* read/write the temperature, includes measured value and limits */
348 show_temp(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
350 struct sensor_device_attribute_2
*sensor_attr
=
351 to_sensor_dev_attr_2(attr
);
352 int nr
= sensor_attr
->nr
;
353 int index
= sensor_attr
->index
;
354 struct w83l786ng_data
*data
= w83l786ng_update_device(dev
);
355 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp
[nr
][index
]));
359 store_temp(struct device
*dev
, struct device_attribute
*attr
,
360 const char *buf
, size_t count
)
362 struct sensor_device_attribute_2
*sensor_attr
=
363 to_sensor_dev_attr_2(attr
);
364 int nr
= sensor_attr
->nr
;
365 int index
= sensor_attr
->index
;
366 struct i2c_client
*client
= to_i2c_client(dev
);
367 struct w83l786ng_data
*data
= i2c_get_clientdata(client
);
370 val
= simple_strtol(buf
, NULL
, 10);
371 mutex_lock(&data
->update_lock
);
372 data
->temp
[nr
][index
] = TEMP_TO_REG(val
);
373 w83l786ng_write_value(client
, W83L786NG_REG_TEMP
[nr
][index
],
374 data
->temp
[nr
][index
]);
375 mutex_unlock(&data
->update_lock
);
380 static struct sensor_device_attribute_2 sda_temp_input
[] = {
381 SENSOR_ATTR_2(temp1_input
, S_IRUGO
, show_temp
, NULL
, 0, 0),
382 SENSOR_ATTR_2(temp2_input
, S_IRUGO
, show_temp
, NULL
, 1, 0),
385 static struct sensor_device_attribute_2 sda_temp_max
[] = {
386 SENSOR_ATTR_2(temp1_max
, S_IRUGO
| S_IWUSR
,
387 show_temp
, store_temp
, 0, 1),
388 SENSOR_ATTR_2(temp2_max
, S_IRUGO
| S_IWUSR
,
389 show_temp
, store_temp
, 1, 1),
392 static struct sensor_device_attribute_2 sda_temp_max_hyst
[] = {
393 SENSOR_ATTR_2(temp1_max_hyst
, S_IRUGO
| S_IWUSR
,
394 show_temp
, store_temp
, 0, 2),
395 SENSOR_ATTR_2(temp2_max_hyst
, S_IRUGO
| S_IWUSR
,
396 show_temp
, store_temp
, 1, 2),
399 #define show_pwm_reg(reg) \
400 static ssize_t show_##reg (struct device *dev, struct device_attribute *attr, \
403 struct w83l786ng_data *data = w83l786ng_update_device(dev); \
404 int nr = to_sensor_dev_attr(attr)->index; \
405 return sprintf(buf, "%d\n", data->reg[nr]); \
408 show_pwm_reg(pwm_mode
)
409 show_pwm_reg(pwm_enable
)
413 store_pwm_mode(struct device
*dev
, struct device_attribute
*attr
,
414 const char *buf
, size_t count
)
416 int nr
= to_sensor_dev_attr(attr
)->index
;
417 struct i2c_client
*client
= to_i2c_client(dev
);
418 struct w83l786ng_data
*data
= i2c_get_clientdata(client
);
419 u32 val
= simple_strtoul(buf
, NULL
, 10);
424 mutex_lock(&data
->update_lock
);
425 data
->pwm_mode
[nr
] = val
;
426 reg
= w83l786ng_read_value(client
, W83L786NG_REG_FAN_CFG
);
427 reg
&= ~(1 << W83L786NG_PWM_MODE_SHIFT
[nr
]);
429 reg
|= 1 << W83L786NG_PWM_MODE_SHIFT
[nr
];
430 w83l786ng_write_value(client
, W83L786NG_REG_FAN_CFG
, reg
);
431 mutex_unlock(&data
->update_lock
);
436 store_pwm(struct device
*dev
, struct device_attribute
*attr
,
437 const char *buf
, size_t count
)
439 int nr
= to_sensor_dev_attr(attr
)->index
;
440 struct i2c_client
*client
= to_i2c_client(dev
);
441 struct w83l786ng_data
*data
= i2c_get_clientdata(client
);
442 u32 val
= SENSORS_LIMIT(simple_strtoul(buf
, NULL
, 10), 0, 255);
444 mutex_lock(&data
->update_lock
);
446 w83l786ng_write_value(client
, W83L786NG_REG_PWM
[nr
], val
);
447 mutex_unlock(&data
->update_lock
);
452 store_pwm_enable(struct device
*dev
, struct device_attribute
*attr
,
453 const char *buf
, size_t count
)
455 int nr
= to_sensor_dev_attr(attr
)->index
;
456 struct i2c_client
*client
= to_i2c_client(dev
);
457 struct w83l786ng_data
*data
= i2c_get_clientdata(client
);
458 u32 val
= simple_strtoul(buf
, NULL
, 10);
462 if (!val
|| (val
> 2)) /* only modes 1 and 2 are supported */
465 mutex_lock(&data
->update_lock
);
466 reg
= w83l786ng_read_value(client
, W83L786NG_REG_FAN_CFG
);
467 data
->pwm_enable
[nr
] = val
;
468 reg
&= ~(0x02 << W83L786NG_PWM_ENABLE_SHIFT
[nr
]);
469 reg
|= (val
- 1) << W83L786NG_PWM_ENABLE_SHIFT
[nr
];
470 w83l786ng_write_value(client
, W83L786NG_REG_FAN_CFG
, reg
);
471 mutex_unlock(&data
->update_lock
);
475 static struct sensor_device_attribute sda_pwm
[] = {
476 SENSOR_ATTR(pwm1
, S_IWUSR
| S_IRUGO
, show_pwm
, store_pwm
, 0),
477 SENSOR_ATTR(pwm2
, S_IWUSR
| S_IRUGO
, show_pwm
, store_pwm
, 1),
480 static struct sensor_device_attribute sda_pwm_mode
[] = {
481 SENSOR_ATTR(pwm1_mode
, S_IWUSR
| S_IRUGO
, show_pwm_mode
,
483 SENSOR_ATTR(pwm2_mode
, S_IWUSR
| S_IRUGO
, show_pwm_mode
,
487 static struct sensor_device_attribute sda_pwm_enable
[] = {
488 SENSOR_ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
, show_pwm_enable
,
489 store_pwm_enable
, 0),
490 SENSOR_ATTR(pwm2_enable
, S_IWUSR
| S_IRUGO
, show_pwm_enable
,
491 store_pwm_enable
, 1),
494 /* For Smart Fan I/Thermal Cruise and Smart Fan II */
496 show_tolerance(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
498 int nr
= to_sensor_dev_attr(attr
)->index
;
499 struct w83l786ng_data
*data
= w83l786ng_update_device(dev
);
500 return sprintf(buf
, "%ld\n", (long)data
->tolerance
[nr
]);
504 store_tolerance(struct device
*dev
, struct device_attribute
*attr
,
505 const char *buf
, size_t count
)
507 int nr
= to_sensor_dev_attr(attr
)->index
;
508 struct i2c_client
*client
= to_i2c_client(dev
);
509 struct w83l786ng_data
*data
= i2c_get_clientdata(client
);
511 u8 tol_tmp
, tol_mask
;
513 val
= simple_strtoul(buf
, NULL
, 10);
515 mutex_lock(&data
->update_lock
);
516 tol_mask
= w83l786ng_read_value(client
,
517 W83L786NG_REG_TOLERANCE
) & ((nr
== 1) ? 0x0f : 0xf0);
518 tol_tmp
= SENSORS_LIMIT(val
, 0, 15);
520 data
->tolerance
[nr
] = tol_tmp
;
525 w83l786ng_write_value(client
, W83L786NG_REG_TOLERANCE
,
527 mutex_unlock(&data
->update_lock
);
531 static struct sensor_device_attribute sda_tolerance
[] = {
532 SENSOR_ATTR(pwm1_tolerance
, S_IWUSR
| S_IRUGO
,
533 show_tolerance
, store_tolerance
, 0),
534 SENSOR_ATTR(pwm2_tolerance
, S_IWUSR
| S_IRUGO
,
535 show_tolerance
, store_tolerance
, 1),
539 #define IN_UNIT_ATTRS(X) \
540 &sda_in_input[X].dev_attr.attr, \
541 &sda_in_min[X].dev_attr.attr, \
542 &sda_in_max[X].dev_attr.attr
544 #define FAN_UNIT_ATTRS(X) \
545 &sda_fan_input[X].dev_attr.attr, \
546 &sda_fan_min[X].dev_attr.attr, \
547 &sda_fan_div[X].dev_attr.attr
549 #define TEMP_UNIT_ATTRS(X) \
550 &sda_temp_input[X].dev_attr.attr, \
551 &sda_temp_max[X].dev_attr.attr, \
552 &sda_temp_max_hyst[X].dev_attr.attr
554 #define PWM_UNIT_ATTRS(X) \
555 &sda_pwm[X].dev_attr.attr, \
556 &sda_pwm_mode[X].dev_attr.attr, \
557 &sda_pwm_enable[X].dev_attr.attr
559 #define TOLERANCE_UNIT_ATTRS(X) \
560 &sda_tolerance[X].dev_attr.attr
562 static struct attribute
*w83l786ng_attributes
[] = {
572 TOLERANCE_UNIT_ATTRS(0),
573 TOLERANCE_UNIT_ATTRS(1),
577 static const struct attribute_group w83l786ng_group
= {
578 .attrs
= w83l786ng_attributes
,
582 w83l786ng_attach_adapter(struct i2c_adapter
*adapter
)
584 if (!(adapter
->class & I2C_CLASS_HWMON
))
586 return i2c_probe(adapter
, &addr_data
, w83l786ng_detect
);
590 w83l786ng_detect(struct i2c_adapter
*adapter
, int address
, int kind
)
592 struct i2c_client
*client
;
594 struct w83l786ng_data
*data
;
598 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
602 /* OK. For now, we presume we have a valid client. We now create the
603 client structure, even though we cannot fill it completely yet.
604 But it allows us to access w83l786ng_{read,write}_value. */
606 if (!(data
= kzalloc(sizeof(struct w83l786ng_data
), GFP_KERNEL
))) {
611 client
= &data
->client
;
613 i2c_set_clientdata(client
, data
);
614 client
->addr
= address
;
615 client
->adapter
= adapter
;
616 client
->driver
= &w83l786ng_driver
;
619 * Now we do the remaining detection. A negative kind means that
620 * the driver was loaded with no force parameter (default), so we
621 * must both detect and identify the chip (actually there is only
622 * one possible kind of chip for now, W83L786NG). A zero kind means
623 * that the driver was loaded with the force parameter, the detection
624 * step shall be skipped. A positive kind means that the driver
625 * was loaded with the force parameter and a given kind of chip is
626 * requested, so both the detection and the identification steps
629 if (kind
< 0) { /* detection */
630 if (((w83l786ng_read_value(client
,
631 W83L786NG_REG_CONFIG
) & 0x80) != 0x00)) {
632 dev_dbg(&adapter
->dev
,
633 "W83L786NG detection failed at 0x%02x.\n",
639 if (kind
<= 0) { /* identification */
643 man_id
= (w83l786ng_read_value(client
,
644 W83L786NG_REG_MAN_ID1
) << 8) +
645 w83l786ng_read_value(client
, W83L786NG_REG_MAN_ID2
);
646 chip_id
= w83l786ng_read_value(client
, W83L786NG_REG_CHIP_ID
);
648 if (man_id
== 0x5CA3) { /* Winbond */
649 if (chip_id
== 0x80) { /* W83L786NG */
654 if (kind
<= 0) { /* identification failed */
655 dev_info(&adapter
->dev
,
656 "Unsupported chip (man_id=0x%04X, "
657 "chip_id=0x%02X).\n", man_id
, chip_id
);
662 /* Fill in the remaining client fields and put into the global list */
663 strlcpy(client
->name
, "w83l786ng", I2C_NAME_SIZE
);
664 mutex_init(&data
->update_lock
);
666 /* Tell the I2C layer a new client has arrived */
667 if ((err
= i2c_attach_client(client
)))
670 /* Initialize the chip */
671 w83l786ng_init_client(client
);
673 /* A few vars need to be filled upon startup */
674 for (i
= 0; i
< 2; i
++) {
675 data
->fan_min
[i
] = w83l786ng_read_value(client
,
676 W83L786NG_REG_FAN_MIN(i
));
679 /* Update the fan divisor */
680 reg_tmp
= w83l786ng_read_value(client
, W83L786NG_REG_FAN_DIV
);
681 data
->fan_div
[0] = reg_tmp
& 0x07;
682 data
->fan_div
[1] = (reg_tmp
>> 4) & 0x07;
684 /* Register sysfs hooks */
685 if ((err
= sysfs_create_group(&client
->dev
.kobj
, &w83l786ng_group
)))
688 data
->hwmon_dev
= hwmon_device_register(dev
);
689 if (IS_ERR(data
->hwmon_dev
)) {
690 err
= PTR_ERR(data
->hwmon_dev
);
696 /* Unregister sysfs hooks */
699 sysfs_remove_group(&client
->dev
.kobj
, &w83l786ng_group
);
700 i2c_detach_client(client
);
708 w83l786ng_detach_client(struct i2c_client
*client
)
710 struct w83l786ng_data
*data
= i2c_get_clientdata(client
);
713 hwmon_device_unregister(data
->hwmon_dev
);
714 sysfs_remove_group(&client
->dev
.kobj
, &w83l786ng_group
);
716 if ((err
= i2c_detach_client(client
)))
725 w83l786ng_init_client(struct i2c_client
*client
)
730 w83l786ng_write_value(client
, W83L786NG_REG_CONFIG
, 0x80);
732 /* Start monitoring */
733 tmp
= w83l786ng_read_value(client
, W83L786NG_REG_CONFIG
);
735 w83l786ng_write_value(client
, W83L786NG_REG_CONFIG
, tmp
| 0x01);
738 static struct w83l786ng_data
*w83l786ng_update_device(struct device
*dev
)
740 struct i2c_client
*client
= to_i2c_client(dev
);
741 struct w83l786ng_data
*data
= i2c_get_clientdata(client
);
745 mutex_lock(&data
->update_lock
);
746 if (time_after(jiffies
, data
->last_updated
+ HZ
+ HZ
/ 2)
748 dev_dbg(&client
->dev
, "Updating w83l786ng data.\n");
750 /* Update the voltages measured value and limits */
751 for (i
= 0; i
< 3; i
++) {
752 data
->in
[i
] = w83l786ng_read_value(client
,
753 W83L786NG_REG_IN(i
));
754 data
->in_min
[i
] = w83l786ng_read_value(client
,
755 W83L786NG_REG_IN_MIN(i
));
756 data
->in_max
[i
] = w83l786ng_read_value(client
,
757 W83L786NG_REG_IN_MAX(i
));
760 /* Update the fan counts and limits */
761 for (i
= 0; i
< 2; i
++) {
762 data
->fan
[i
] = w83l786ng_read_value(client
,
763 W83L786NG_REG_FAN(i
));
764 data
->fan_min
[i
] = w83l786ng_read_value(client
,
765 W83L786NG_REG_FAN_MIN(i
));
768 /* Update the fan divisor */
769 reg_tmp
= w83l786ng_read_value(client
, W83L786NG_REG_FAN_DIV
);
770 data
->fan_div
[0] = reg_tmp
& 0x07;
771 data
->fan_div
[1] = (reg_tmp
>> 4) & 0x07;
773 pwmcfg
= w83l786ng_read_value(client
, W83L786NG_REG_FAN_CFG
);
774 for (i
= 0; i
< 2; i
++) {
776 ((pwmcfg
>> W83L786NG_PWM_MODE_SHIFT
[i
]) & 1)
778 data
->pwm_enable
[i
] =
779 ((pwmcfg
>> W83L786NG_PWM_ENABLE_SHIFT
[i
]) & 2) + 1;
780 data
->pwm
[i
] = w83l786ng_read_value(client
,
781 W83L786NG_REG_PWM
[i
]);
785 /* Update the temperature sensors */
786 for (i
= 0; i
< 2; i
++) {
787 for (j
= 0; j
< 3; j
++) {
788 data
->temp
[i
][j
] = w83l786ng_read_value(client
,
789 W83L786NG_REG_TEMP
[i
][j
]);
793 /* Update Smart Fan I/II tolerance */
794 reg_tmp
= w83l786ng_read_value(client
, W83L786NG_REG_TOLERANCE
);
795 data
->tolerance
[0] = reg_tmp
& 0x0f;
796 data
->tolerance
[1] = (reg_tmp
>> 4) & 0x0f;
798 data
->last_updated
= jiffies
;
803 mutex_unlock(&data
->update_lock
);
809 sensors_w83l786ng_init(void)
811 return i2c_add_driver(&w83l786ng_driver
);
815 sensors_w83l786ng_exit(void)
817 i2c_del_driver(&w83l786ng_driver
);
820 MODULE_AUTHOR("Kevin Lo");
821 MODULE_DESCRIPTION("w83l786ng driver");
822 MODULE_LICENSE("GPL");
824 module_init(sensors_w83l786ng_init
);
825 module_exit(sensors_w83l786ng_exit
);