Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hwmon / w83l786ng.c
blobfd61ba7588373a44958bec656360f45d4296cf3f
1 /*
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
17 02110-1301 USA.
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 };
40 =======
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);
47 static int reset;
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};
88 static inline u8
89 FAN_TO_REG(long rpm, int div)
91 if (rpm == 0)
92 return 255;
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 : \
98 ((val) == 255 ? 0 : \
99 1350000 / ((val) * (div))))
101 /* for temp */
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))
114 static inline u8
115 DIV_TO_REG(long val)
117 int i;
118 val = SENSORS_LIMIT(val, 1, 128) >> 1;
119 for (i = 0; i < 7; i++) {
120 if (val == 0)
121 break;
122 val >>= 1;
124 return ((u8) 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 */
136 u8 in[3];
137 u8 in_max[3];
138 u8 in_min[3];
139 u8 fan[2];
140 u8 fan_div[2];
141 u8 fan_min[2];
142 u8 temp_type[2];
143 u8 temp[2][3];
144 u8 pwm[2];
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) */
150 u8 tolerance[2];
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 = {
160 .driver = {
161 .name = "w83l786ng",
163 .attach_adapter = w83l786ng_attach_adapter,
164 .detach_client = w83l786ng_detach_client,
167 static u8
168 w83l786ng_read_value(struct i2c_client *client, u8 reg)
170 return i2c_smbus_read_byte_data(client, reg);
173 static int
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) \
181 static ssize_t \
182 show_##reg(struct device *dev, struct device_attribute *attr, \
183 char *buf) \
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])); \
190 show_in_reg(in)
191 show_in_reg(in_min)
192 show_in_reg(in_max)
194 #define store_in_reg(REG, reg) \
195 static ssize_t \
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); \
208 return count; \
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, \
234 char *buf) \
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]))); \
242 show_fan_reg(fan);
243 show_fan_reg(fan_min);
245 static ssize_t
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);
252 u32 val;
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),
258 data->fan_min[nr]);
259 mutex_unlock(&data->update_lock);
261 return count;
264 static ssize_t
265 show_fan_div(struct device *dev, struct device_attribute *attr,
266 char *buf)
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. */
277 static ssize_t
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);
285 unsigned long min;
286 u8 tmp_fan_div;
287 u8 fan_div_reg;
288 u8 keep_mask = 0;
289 u8 new_shift = 0;
291 /* Save fan_min */
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));
297 switch (nr) {
298 case 0:
299 keep_mask = 0xf8;
300 new_shift = 0;
301 break;
302 case 1:
303 keep_mask = 0x8f;
304 new_shift = 4;
305 break;
308 fan_div_reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV)
309 & keep_mask;
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),
319 data->fan_min[nr]);
320 mutex_unlock(&data->update_lock);
322 return count;
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,
332 store_fan_min, 0),
333 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
334 store_fan_min, 1),
337 static struct sensor_device_attribute sda_fan_div[] = {
338 SENSOR_ATTR(fan1_div, S_IWUSR | S_IRUGO, show_fan_div,
339 store_fan_div, 0),
340 SENSOR_ATTR(fan2_div, S_IWUSR | S_IRUGO, show_fan_div,
341 store_fan_div, 1),
345 /* read/write the temperature, includes measured value and limits */
347 static ssize_t
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]));
358 static ssize_t
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);
368 s32 val;
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);
377 return count;
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, \
401 char *buf) \
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)
410 show_pwm_reg(pwm)
412 static ssize_t
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);
420 u8 reg;
422 if (val > 1)
423 return -EINVAL;
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]);
428 if (!val)
429 reg |= 1 << W83L786NG_PWM_MODE_SHIFT[nr];
430 w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg);
431 mutex_unlock(&data->update_lock);
432 return count;
435 static ssize_t
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);
445 data->pwm[nr] = val;
446 w83l786ng_write_value(client, W83L786NG_REG_PWM[nr], val);
447 mutex_unlock(&data->update_lock);
448 return count;
451 static ssize_t
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);
460 u8 reg;
462 if (!val || (val > 2)) /* only modes 1 and 2 are supported */
463 return -EINVAL;
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);
472 return count;
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,
482 store_pwm_mode, 0),
483 SENSOR_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
484 store_pwm_mode, 1),
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 */
495 static ssize_t
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]);
503 static ssize_t
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);
510 u32 val;
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);
519 tol_tmp &= 0x0f;
520 data->tolerance[nr] = tol_tmp;
521 if (nr == 1) {
522 tol_tmp <<= 4;
525 w83l786ng_write_value(client, W83L786NG_REG_TOLERANCE,
526 tol_mask | tol_tmp);
527 mutex_unlock(&data->update_lock);
528 return count;
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[] = {
563 IN_UNIT_ATTRS(0),
564 IN_UNIT_ATTRS(1),
565 IN_UNIT_ATTRS(2),
566 FAN_UNIT_ATTRS(0),
567 FAN_UNIT_ATTRS(1),
568 TEMP_UNIT_ATTRS(0),
569 TEMP_UNIT_ATTRS(1),
570 PWM_UNIT_ATTRS(0),
571 PWM_UNIT_ATTRS(1),
572 TOLERANCE_UNIT_ATTRS(0),
573 TOLERANCE_UNIT_ATTRS(1),
574 NULL
577 static const struct attribute_group w83l786ng_group = {
578 .attrs = w83l786ng_attributes,
581 static int
582 w83l786ng_attach_adapter(struct i2c_adapter *adapter)
584 if (!(adapter->class & I2C_CLASS_HWMON))
585 return 0;
586 return i2c_probe(adapter, &addr_data, w83l786ng_detect);
589 static int
590 w83l786ng_detect(struct i2c_adapter *adapter, int address, int kind)
592 struct i2c_client *client;
593 struct device *dev;
594 struct w83l786ng_data *data;
595 int i, err = 0;
596 u8 reg_tmp;
598 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
599 goto exit;
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))) {
607 err = -ENOMEM;
608 goto exit;
611 client = &data->client;
612 dev = &client->dev;
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
627 * are skipped.
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",
634 address);
635 goto exit_free;
639 if (kind <= 0) { /* identification */
640 u16 man_id;
641 u8 chip_id;
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 */
650 kind = 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);
658 goto exit_free;
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)))
668 goto exit_free;
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)))
686 goto exit_remove;
688 data->hwmon_dev = hwmon_device_register(dev);
689 if (IS_ERR(data->hwmon_dev)) {
690 err = PTR_ERR(data->hwmon_dev);
691 goto exit_remove;
694 return 0;
696 /* Unregister sysfs hooks */
698 exit_remove:
699 sysfs_remove_group(&client->dev.kobj, &w83l786ng_group);
700 i2c_detach_client(client);
701 exit_free:
702 kfree(data);
703 exit:
704 return err;
707 static int
708 w83l786ng_detach_client(struct i2c_client *client)
710 struct w83l786ng_data *data = i2c_get_clientdata(client);
711 int err;
713 hwmon_device_unregister(data->hwmon_dev);
714 sysfs_remove_group(&client->dev.kobj, &w83l786ng_group);
716 if ((err = i2c_detach_client(client)))
717 return err;
719 kfree(data);
721 return 0;
724 static void
725 w83l786ng_init_client(struct i2c_client *client)
727 u8 tmp;
729 if (reset)
730 w83l786ng_write_value(client, W83L786NG_REG_CONFIG, 0x80);
732 /* Start monitoring */
733 tmp = w83l786ng_read_value(client, W83L786NG_REG_CONFIG);
734 if (!(tmp & 0x01))
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);
742 int i, j;
743 u8 reg_tmp, pwmcfg;
745 mutex_lock(&data->update_lock);
746 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
747 || !data->valid) {
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++) {
775 data->pwm_mode[i] =
776 ((pwmcfg >> W83L786NG_PWM_MODE_SHIFT[i]) & 1)
777 ? 0 : 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;
799 data->valid = 1;
803 mutex_unlock(&data->update_lock);
805 return data;
808 static int __init
809 sensors_w83l786ng_init(void)
811 return i2c_add_driver(&w83l786ng_driver);
814 static void __exit
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);