RDMA/ucma: Correct option size check using optlen
[pandora-kernel.git] / drivers / hwmon / w83l786ng.c
1 /*
2     w83l786ng.c - Linux kernel driver for hardware monitoring
3     Copyright (c) 2007 Kevin Lo <kevlo@kevlo.org>
4
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.
8
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.
13
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.
18 */
19
20 /*
21     Supports following chips:
22
23     Chip        #vin    #fanin  #pwm    #temp   wchipid vendid  i2c     ISA
24     w83l786ng   3       2       2       2       0x7b    0x5ca3  yes     no
25 */
26
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>
36
37 /* Addresses to scan */
38 static const unsigned short normal_i2c[] = { 0x2e, 0x2f, I2C_CLIENT_END };
39
40 /* Insmod parameters */
41
42 static int reset;
43 module_param(reset, bool, 0);
44 MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
45
46 #define W83L786NG_REG_IN_MIN(nr)        (0x2C + (nr) * 2)
47 #define W83L786NG_REG_IN_MAX(nr)        (0x2B + (nr) * 2)
48 #define W83L786NG_REG_IN(nr)            ((nr) + 0x20)
49
50 #define W83L786NG_REG_FAN(nr)           ((nr) + 0x28)
51 #define W83L786NG_REG_FAN_MIN(nr)       ((nr) + 0x3B)
52
53 #define W83L786NG_REG_CONFIG            0x40
54 #define W83L786NG_REG_ALARM1            0x41
55 #define W83L786NG_REG_ALARM2            0x42
56 #define W83L786NG_REG_GPIO_EN           0x47
57 #define W83L786NG_REG_MAN_ID2           0x4C
58 #define W83L786NG_REG_MAN_ID1           0x4D
59 #define W83L786NG_REG_CHIP_ID           0x4E
60
61 #define W83L786NG_REG_DIODE             0x53
62 #define W83L786NG_REG_FAN_DIV           0x54
63 #define W83L786NG_REG_FAN_CFG           0x80
64
65 #define W83L786NG_REG_TOLERANCE         0x8D
66
67 static const u8 W83L786NG_REG_TEMP[2][3] = {
68         { 0x25,         /* TEMP 0 in DataSheet */
69           0x35,         /* TEMP 0 Over in DataSheet */
70           0x36 },       /* TEMP 0 Hyst in DataSheet */
71         { 0x26,         /* TEMP 1 in DataSheet */
72           0x37,         /* TEMP 1 Over in DataSheet */
73           0x38 }        /* TEMP 1 Hyst in DataSheet */
74 };
75
76 static const u8 W83L786NG_PWM_MODE_SHIFT[] = {6, 7};
77 static const u8 W83L786NG_PWM_ENABLE_SHIFT[] = {2, 4};
78
79 /* FAN Duty Cycle, be used to control */
80 static const u8 W83L786NG_REG_PWM[] = {0x81, 0x87};
81
82
83 static inline u8
84 FAN_TO_REG(long rpm, int div)
85 {
86         if (rpm == 0)
87                 return 255;
88         rpm = SENSORS_LIMIT(rpm, 1, 1000000);
89         return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
90 }
91
92 #define FAN_FROM_REG(val,div)   ((val) == 0   ? -1 : \
93                                 ((val) == 255 ? 0 : \
94                                 1350000 / ((val) * (div))))
95
96 /* for temp */
97 #define TEMP_TO_REG(val)        (SENSORS_LIMIT(((val) < 0 ? (val)+0x100*1000 \
98                                     : (val)) / 1000, 0, 0xff))
99 #define TEMP_FROM_REG(val)      (((val) & 0x80 ? (val)-0x100 : (val)) * 1000)
100
101 /* The analog voltage inputs have 8mV LSB. Since the sysfs output is
102    in mV as would be measured on the chip input pin, need to just
103    multiply/divide by 8 to translate from/to register values. */
104 #define IN_TO_REG(val)          (SENSORS_LIMIT((((val) + 4) / 8), 0, 255))
105 #define IN_FROM_REG(val)        ((val) * 8)
106
107 #define DIV_FROM_REG(val)       (1 << (val))
108
109 static inline u8
110 DIV_TO_REG(long val)
111 {
112         int i;
113         val = SENSORS_LIMIT(val, 1, 128) >> 1;
114         for (i = 0; i < 7; i++) {
115                 if (val == 0)
116                         break;
117                 val >>= 1;
118         }
119         return ((u8) i);
120 }
121
122 struct w83l786ng_data {
123         struct device *hwmon_dev;
124         struct mutex update_lock;
125         char valid;                     /* !=0 if following fields are valid */
126         unsigned long last_updated;     /* In jiffies */
127         unsigned long last_nonvolatile; /* In jiffies, last time we update the
128                                            nonvolatile registers */
129
130         u8 in[3];
131         u8 in_max[3];
132         u8 in_min[3];
133         u8 fan[2];
134         u8 fan_div[2];
135         u8 fan_min[2];
136         u8 temp_type[2];
137         u8 temp[2][3];
138         u8 pwm[2];
139         u8 pwm_mode[2]; /* 0->DC variable voltage
140                            1->PWM variable duty cycle */
141
142         u8 pwm_enable[2]; /* 1->manual
143                              2->thermal cruise (also called SmartFan I) */
144         u8 tolerance[2];
145 };
146
147 static int w83l786ng_probe(struct i2c_client *client,
148                            const struct i2c_device_id *id);
149 static int w83l786ng_detect(struct i2c_client *client,
150                             struct i2c_board_info *info);
151 static int w83l786ng_remove(struct i2c_client *client);
152 static void w83l786ng_init_client(struct i2c_client *client);
153 static struct w83l786ng_data *w83l786ng_update_device(struct device *dev);
154
155 static const struct i2c_device_id w83l786ng_id[] = {
156         { "w83l786ng", 0 },
157         { }
158 };
159 MODULE_DEVICE_TABLE(i2c, w83l786ng_id);
160
161 static struct i2c_driver w83l786ng_driver = {
162         .class          = I2C_CLASS_HWMON,
163         .driver = {
164                    .name = "w83l786ng",
165         },
166         .probe          = w83l786ng_probe,
167         .remove         = w83l786ng_remove,
168         .id_table       = w83l786ng_id,
169         .detect         = w83l786ng_detect,
170         .address_list   = normal_i2c,
171 };
172
173 static u8
174 w83l786ng_read_value(struct i2c_client *client, u8 reg)
175 {
176         return i2c_smbus_read_byte_data(client, reg);
177 }
178
179 static int
180 w83l786ng_write_value(struct i2c_client *client, u8 reg, u8 value)
181 {
182         return i2c_smbus_write_byte_data(client, reg, value);
183 }
184
185 /* following are the sysfs callback functions */
186 #define show_in_reg(reg) \
187 static ssize_t \
188 show_##reg(struct device *dev, struct device_attribute *attr, \
189            char *buf) \
190 { \
191         int nr = to_sensor_dev_attr(attr)->index; \
192         struct w83l786ng_data *data = w83l786ng_update_device(dev); \
193         return sprintf(buf,"%d\n", IN_FROM_REG(data->reg[nr])); \
194 }
195
196 show_in_reg(in)
197 show_in_reg(in_min)
198 show_in_reg(in_max)
199
200 #define store_in_reg(REG, reg) \
201 static ssize_t \
202 store_in_##reg (struct device *dev, struct device_attribute *attr, \
203                 const char *buf, size_t count) \
204 { \
205         int nr = to_sensor_dev_attr(attr)->index; \
206         struct i2c_client *client = to_i2c_client(dev); \
207         struct w83l786ng_data *data = i2c_get_clientdata(client); \
208         unsigned long val = simple_strtoul(buf, NULL, 10); \
209         mutex_lock(&data->update_lock); \
210         data->in_##reg[nr] = IN_TO_REG(val); \
211         w83l786ng_write_value(client, W83L786NG_REG_IN_##REG(nr), \
212                               data->in_##reg[nr]); \
213         mutex_unlock(&data->update_lock); \
214         return count; \
215 }
216
217 store_in_reg(MIN, min)
218 store_in_reg(MAX, max)
219
220 static struct sensor_device_attribute sda_in_input[] = {
221         SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
222         SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
223         SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
224 };
225
226 static struct sensor_device_attribute sda_in_min[] = {
227         SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0),
228         SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1),
229         SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2),
230 };
231
232 static struct sensor_device_attribute sda_in_max[] = {
233         SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0),
234         SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1),
235         SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2),
236 };
237
238 #define show_fan_reg(reg) \
239 static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
240                           char *buf) \
241 { \
242         int nr = to_sensor_dev_attr(attr)->index; \
243         struct w83l786ng_data *data = w83l786ng_update_device(dev); \
244         return sprintf(buf,"%d\n", \
245                 FAN_FROM_REG(data->fan[nr], DIV_FROM_REG(data->fan_div[nr]))); \
246 }
247
248 show_fan_reg(fan);
249 show_fan_reg(fan_min);
250
251 static ssize_t
252 store_fan_min(struct device *dev, struct device_attribute *attr,
253               const char *buf, size_t count)
254 {
255         int nr = to_sensor_dev_attr(attr)->index;
256         struct i2c_client *client = to_i2c_client(dev);
257         struct w83l786ng_data *data = i2c_get_clientdata(client);
258         u32 val;
259
260         val = simple_strtoul(buf, NULL, 10);
261         mutex_lock(&data->update_lock);
262         data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
263         w83l786ng_write_value(client, W83L786NG_REG_FAN_MIN(nr),
264                               data->fan_min[nr]);
265         mutex_unlock(&data->update_lock);
266
267         return count;
268 }
269
270 static ssize_t
271 show_fan_div(struct device *dev, struct device_attribute *attr,
272              char *buf)
273 {
274         int nr = to_sensor_dev_attr(attr)->index;
275         struct w83l786ng_data *data = w83l786ng_update_device(dev);
276         return sprintf(buf, "%u\n", DIV_FROM_REG(data->fan_div[nr]));
277 }
278
279 /* Note: we save and restore the fan minimum here, because its value is
280    determined in part by the fan divisor.  This follows the principle of
281    least surprise; the user doesn't expect the fan minimum to change just
282    because the divisor changed. */
283 static ssize_t
284 store_fan_div(struct device *dev, struct device_attribute *attr,
285               const char *buf, size_t count)
286 {
287         int nr = to_sensor_dev_attr(attr)->index;
288         struct i2c_client *client = to_i2c_client(dev);
289         struct w83l786ng_data *data = i2c_get_clientdata(client);
290
291         unsigned long min;
292         u8 tmp_fan_div;
293         u8 fan_div_reg;
294         u8 keep_mask = 0;
295         u8 new_shift = 0;
296
297         /* Save fan_min */
298         mutex_lock(&data->update_lock);
299         min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
300
301         data->fan_div[nr] = DIV_TO_REG(simple_strtoul(buf, NULL, 10));
302
303         switch (nr) {
304         case 0:
305                 keep_mask = 0xf8;
306                 new_shift = 0;
307                 break;
308         case 1:
309                 keep_mask = 0x8f;
310                 new_shift = 4;
311                 break;
312         }
313
314         fan_div_reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV)
315                                            & keep_mask;
316
317         tmp_fan_div = (data->fan_div[nr] << new_shift) & ~keep_mask;
318
319         w83l786ng_write_value(client, W83L786NG_REG_FAN_DIV,
320                               fan_div_reg | tmp_fan_div);
321
322         /* Restore fan_min */
323         data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
324         w83l786ng_write_value(client, W83L786NG_REG_FAN_MIN(nr),
325                               data->fan_min[nr]);
326         mutex_unlock(&data->update_lock);
327
328         return count;
329 }
330
331 static struct sensor_device_attribute sda_fan_input[] = {
332         SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
333         SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
334 };
335
336 static struct sensor_device_attribute sda_fan_min[] = {
337         SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
338                     store_fan_min, 0),
339         SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
340                     store_fan_min, 1),
341 };
342
343 static struct sensor_device_attribute sda_fan_div[] = {
344         SENSOR_ATTR(fan1_div, S_IWUSR | S_IRUGO, show_fan_div,
345                     store_fan_div, 0),
346         SENSOR_ATTR(fan2_div, S_IWUSR | S_IRUGO, show_fan_div,
347                     store_fan_div, 1),
348 };
349
350
351 /* read/write the temperature, includes measured value and limits */
352
353 static ssize_t
354 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
355 {
356         struct sensor_device_attribute_2 *sensor_attr =
357             to_sensor_dev_attr_2(attr);
358         int nr = sensor_attr->nr;
359         int index = sensor_attr->index;
360         struct w83l786ng_data *data = w83l786ng_update_device(dev);
361         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
362 }
363
364 static ssize_t
365 store_temp(struct device *dev, struct device_attribute *attr,
366            const char *buf, size_t count)
367 {
368         struct sensor_device_attribute_2 *sensor_attr =
369             to_sensor_dev_attr_2(attr);
370         int nr = sensor_attr->nr;
371         int index = sensor_attr->index;
372         struct i2c_client *client = to_i2c_client(dev);
373         struct w83l786ng_data *data = i2c_get_clientdata(client);
374         s32 val;
375
376         val = simple_strtol(buf, NULL, 10);
377         mutex_lock(&data->update_lock);
378         data->temp[nr][index] = TEMP_TO_REG(val);
379         w83l786ng_write_value(client, W83L786NG_REG_TEMP[nr][index],
380                               data->temp[nr][index]);
381         mutex_unlock(&data->update_lock);
382
383         return count;
384 }
385
386 static struct sensor_device_attribute_2 sda_temp_input[] = {
387         SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
388         SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0),
389 };
390
391 static struct sensor_device_attribute_2 sda_temp_max[] = {
392         SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR,
393                       show_temp, store_temp, 0, 1),
394         SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR,
395                       show_temp, store_temp, 1, 1),
396 };
397
398 static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
399         SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR,
400                       show_temp, store_temp, 0, 2),
401         SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR,
402                       show_temp, store_temp, 1, 2),
403 };
404
405 #define show_pwm_reg(reg) \
406 static ssize_t show_##reg (struct device *dev, struct device_attribute *attr, \
407                            char *buf) \
408 { \
409         struct w83l786ng_data *data = w83l786ng_update_device(dev); \
410         int nr = to_sensor_dev_attr(attr)->index; \
411         return sprintf(buf, "%d\n", data->reg[nr]); \
412 }
413
414 show_pwm_reg(pwm_mode)
415 show_pwm_reg(pwm_enable)
416 show_pwm_reg(pwm)
417
418 static ssize_t
419 store_pwm_mode(struct device *dev, struct device_attribute *attr,
420                const char *buf, size_t count)
421 {
422         int nr = to_sensor_dev_attr(attr)->index;
423         struct i2c_client *client = to_i2c_client(dev);
424         struct w83l786ng_data *data = i2c_get_clientdata(client);
425         u32 val = simple_strtoul(buf, NULL, 10);
426         u8 reg;
427
428         if (val > 1)
429                 return -EINVAL;
430         mutex_lock(&data->update_lock);
431         data->pwm_mode[nr] = val;
432         reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
433         reg &= ~(1 << W83L786NG_PWM_MODE_SHIFT[nr]);
434         if (!val)
435                 reg |= 1 << W83L786NG_PWM_MODE_SHIFT[nr];
436         w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg);
437         mutex_unlock(&data->update_lock);
438         return count;
439 }
440
441 static ssize_t
442 store_pwm(struct device *dev, struct device_attribute *attr,
443           const char *buf, size_t count)
444 {
445         int nr = to_sensor_dev_attr(attr)->index;
446         struct i2c_client *client = to_i2c_client(dev);
447         struct w83l786ng_data *data = i2c_get_clientdata(client);
448         u32 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 255);
449
450         val = DIV_ROUND_CLOSEST(val, 0x11);
451
452         mutex_lock(&data->update_lock);
453         data->pwm[nr] = val * 0x11;
454         val |= w83l786ng_read_value(client, W83L786NG_REG_PWM[nr]) & 0xf0;
455         w83l786ng_write_value(client, W83L786NG_REG_PWM[nr], val);
456         mutex_unlock(&data->update_lock);
457         return count;
458 }
459
460 static ssize_t
461 store_pwm_enable(struct device *dev, struct device_attribute *attr,
462                  const char *buf, size_t count)
463 {
464         int nr = to_sensor_dev_attr(attr)->index;
465         struct i2c_client *client = to_i2c_client(dev);
466         struct w83l786ng_data *data = i2c_get_clientdata(client);
467         u32 val = simple_strtoul(buf, NULL, 10);
468
469         u8 reg;
470
471         if (!val || (val > 2))  /* only modes 1 and 2 are supported */
472                 return -EINVAL;
473
474         mutex_lock(&data->update_lock);
475         reg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
476         data->pwm_enable[nr] = val;
477         reg &= ~(0x03 << W83L786NG_PWM_ENABLE_SHIFT[nr]);
478         reg |= (val - 1) << W83L786NG_PWM_ENABLE_SHIFT[nr];
479         w83l786ng_write_value(client, W83L786NG_REG_FAN_CFG, reg);
480         mutex_unlock(&data->update_lock);
481         return count;
482 }
483
484 static struct sensor_device_attribute sda_pwm[] = {
485         SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0),
486         SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1),
487 };
488
489 static struct sensor_device_attribute sda_pwm_mode[] = {
490         SENSOR_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
491                     store_pwm_mode, 0),
492         SENSOR_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
493                     store_pwm_mode, 1),
494 };
495
496 static struct sensor_device_attribute sda_pwm_enable[] = {
497         SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
498                     store_pwm_enable, 0),
499         SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
500                     store_pwm_enable, 1),
501 };
502
503 /* For Smart Fan I/Thermal Cruise and Smart Fan II */
504 static ssize_t
505 show_tolerance(struct device *dev, struct device_attribute *attr, char *buf)
506 {
507         int nr = to_sensor_dev_attr(attr)->index;
508         struct w83l786ng_data *data = w83l786ng_update_device(dev);
509         return sprintf(buf, "%ld\n", (long)data->tolerance[nr]);
510 }
511
512 static ssize_t
513 store_tolerance(struct device *dev, struct device_attribute *attr,
514                 const char *buf, size_t count)
515 {
516         int nr = to_sensor_dev_attr(attr)->index;
517         struct i2c_client *client = to_i2c_client(dev);
518         struct w83l786ng_data *data = i2c_get_clientdata(client);
519         u32 val;
520         u8 tol_tmp, tol_mask;
521
522         val = simple_strtoul(buf, NULL, 10);
523
524         mutex_lock(&data->update_lock);
525         tol_mask = w83l786ng_read_value(client,
526             W83L786NG_REG_TOLERANCE) & ((nr == 1) ? 0x0f : 0xf0);
527         tol_tmp = SENSORS_LIMIT(val, 0, 15);
528         tol_tmp &= 0x0f;
529         data->tolerance[nr] = tol_tmp;
530         if (nr == 1) {
531                 tol_tmp <<= 4;
532         }
533
534         w83l786ng_write_value(client, W83L786NG_REG_TOLERANCE,
535                               tol_mask | tol_tmp);
536         mutex_unlock(&data->update_lock);
537         return count;
538 }
539
540 static struct sensor_device_attribute sda_tolerance[] = {
541         SENSOR_ATTR(pwm1_tolerance, S_IWUSR | S_IRUGO,
542                     show_tolerance, store_tolerance, 0),
543         SENSOR_ATTR(pwm2_tolerance, S_IWUSR | S_IRUGO,
544                     show_tolerance, store_tolerance, 1),
545 };
546
547
548 #define IN_UNIT_ATTRS(X)        \
549         &sda_in_input[X].dev_attr.attr,         \
550         &sda_in_min[X].dev_attr.attr,           \
551         &sda_in_max[X].dev_attr.attr
552
553 #define FAN_UNIT_ATTRS(X)       \
554         &sda_fan_input[X].dev_attr.attr,        \
555         &sda_fan_min[X].dev_attr.attr,          \
556         &sda_fan_div[X].dev_attr.attr
557
558 #define TEMP_UNIT_ATTRS(X)      \
559         &sda_temp_input[X].dev_attr.attr,       \
560         &sda_temp_max[X].dev_attr.attr,         \
561         &sda_temp_max_hyst[X].dev_attr.attr
562
563 #define PWM_UNIT_ATTRS(X)       \
564         &sda_pwm[X].dev_attr.attr,              \
565         &sda_pwm_mode[X].dev_attr.attr,         \
566         &sda_pwm_enable[X].dev_attr.attr
567
568 #define TOLERANCE_UNIT_ATTRS(X) \
569         &sda_tolerance[X].dev_attr.attr
570
571 static struct attribute *w83l786ng_attributes[] = {
572         IN_UNIT_ATTRS(0),
573         IN_UNIT_ATTRS(1),
574         IN_UNIT_ATTRS(2),
575         FAN_UNIT_ATTRS(0),
576         FAN_UNIT_ATTRS(1),
577         TEMP_UNIT_ATTRS(0),
578         TEMP_UNIT_ATTRS(1),
579         PWM_UNIT_ATTRS(0),
580         PWM_UNIT_ATTRS(1),
581         TOLERANCE_UNIT_ATTRS(0),
582         TOLERANCE_UNIT_ATTRS(1),
583         NULL
584 };
585
586 static const struct attribute_group w83l786ng_group = {
587         .attrs = w83l786ng_attributes,
588 };
589
590 static int
591 w83l786ng_detect(struct i2c_client *client, struct i2c_board_info *info)
592 {
593         struct i2c_adapter *adapter = client->adapter;
594         u16 man_id;
595         u8 chip_id;
596
597         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
598                 return -ENODEV;
599         }
600
601         /* Detection */
602         if ((w83l786ng_read_value(client, W83L786NG_REG_CONFIG) & 0x80)) {
603                 dev_dbg(&adapter->dev, "W83L786NG detection failed at 0x%02x\n",
604                         client->addr);
605                 return -ENODEV;
606         }
607
608         /* Identification */
609         man_id = (w83l786ng_read_value(client, W83L786NG_REG_MAN_ID1) << 8) +
610                  w83l786ng_read_value(client, W83L786NG_REG_MAN_ID2);
611         chip_id = w83l786ng_read_value(client, W83L786NG_REG_CHIP_ID);
612
613         if (man_id != 0x5CA3 ||         /* Winbond */
614             chip_id != 0x80) {          /* W83L786NG */
615                 dev_dbg(&adapter->dev,
616                         "Unsupported chip (man_id=0x%04X, chip_id=0x%02X)\n",
617                         man_id, chip_id);
618                 return -ENODEV;
619         }
620
621         strlcpy(info->type, "w83l786ng", I2C_NAME_SIZE);
622
623         return 0;
624 }
625
626 static int
627 w83l786ng_probe(struct i2c_client *client, const struct i2c_device_id *id)
628 {
629         struct device *dev = &client->dev;
630         struct w83l786ng_data *data;
631         int i, err = 0;
632         u8 reg_tmp;
633
634         data = kzalloc(sizeof(struct w83l786ng_data), GFP_KERNEL);
635         if (!data) {
636                 err = -ENOMEM;
637                 goto exit;
638         }
639
640         i2c_set_clientdata(client, data);
641         mutex_init(&data->update_lock);
642
643         /* Initialize the chip */
644         w83l786ng_init_client(client);
645
646         /* A few vars need to be filled upon startup */
647         for (i = 0; i < 2; i++) {
648                 data->fan_min[i] = w83l786ng_read_value(client,
649                     W83L786NG_REG_FAN_MIN(i));
650         }
651
652         /* Update the fan divisor */
653         reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV);
654         data->fan_div[0] = reg_tmp & 0x07;
655         data->fan_div[1] = (reg_tmp >> 4) & 0x07;
656
657         /* Register sysfs hooks */
658         if ((err = sysfs_create_group(&client->dev.kobj, &w83l786ng_group)))
659                 goto exit_remove;
660
661         data->hwmon_dev = hwmon_device_register(dev);
662         if (IS_ERR(data->hwmon_dev)) {
663                 err = PTR_ERR(data->hwmon_dev);
664                 goto exit_remove;
665         }
666
667         return 0;
668
669         /* Unregister sysfs hooks */
670
671 exit_remove:
672         sysfs_remove_group(&client->dev.kobj, &w83l786ng_group);
673         kfree(data);
674 exit:
675         return err;
676 }
677
678 static int
679 w83l786ng_remove(struct i2c_client *client)
680 {
681         struct w83l786ng_data *data = i2c_get_clientdata(client);
682
683         hwmon_device_unregister(data->hwmon_dev);
684         sysfs_remove_group(&client->dev.kobj, &w83l786ng_group);
685
686         kfree(data);
687
688         return 0;
689 }
690
691 static void
692 w83l786ng_init_client(struct i2c_client *client)
693 {
694         u8 tmp;
695
696         if (reset)
697                 w83l786ng_write_value(client, W83L786NG_REG_CONFIG, 0x80);
698
699         /* Start monitoring */
700         tmp = w83l786ng_read_value(client, W83L786NG_REG_CONFIG);
701         if (!(tmp & 0x01))
702                 w83l786ng_write_value(client, W83L786NG_REG_CONFIG, tmp | 0x01);
703 }
704
705 static struct w83l786ng_data *w83l786ng_update_device(struct device *dev)
706 {
707         struct i2c_client *client = to_i2c_client(dev);
708         struct w83l786ng_data *data = i2c_get_clientdata(client);
709         int i, j;
710         u8 reg_tmp, pwmcfg;
711
712         mutex_lock(&data->update_lock);
713         if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
714             || !data->valid) {
715                 dev_dbg(&client->dev, "Updating w83l786ng data.\n");
716
717                 /* Update the voltages measured value and limits */
718                 for (i = 0; i < 3; i++) {
719                         data->in[i] = w83l786ng_read_value(client,
720                             W83L786NG_REG_IN(i));
721                         data->in_min[i] = w83l786ng_read_value(client,
722                             W83L786NG_REG_IN_MIN(i));
723                         data->in_max[i] = w83l786ng_read_value(client,
724                             W83L786NG_REG_IN_MAX(i));
725                 }
726
727                 /* Update the fan counts and limits */
728                 for (i = 0; i < 2; i++) {
729                         data->fan[i] = w83l786ng_read_value(client,
730                             W83L786NG_REG_FAN(i));
731                         data->fan_min[i] = w83l786ng_read_value(client,
732                             W83L786NG_REG_FAN_MIN(i));
733                 }
734
735                 /* Update the fan divisor */
736                 reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_FAN_DIV);
737                 data->fan_div[0] = reg_tmp & 0x07;
738                 data->fan_div[1] = (reg_tmp >> 4) & 0x07;
739
740                 pwmcfg = w83l786ng_read_value(client, W83L786NG_REG_FAN_CFG);
741                 for (i = 0; i < 2; i++) {
742                         data->pwm_mode[i] =
743                             ((pwmcfg >> W83L786NG_PWM_MODE_SHIFT[i]) & 1)
744                             ? 0 : 1;
745                         data->pwm_enable[i] =
746                             ((pwmcfg >> W83L786NG_PWM_ENABLE_SHIFT[i]) & 3) + 1;
747                         data->pwm[i] =
748                             (w83l786ng_read_value(client, W83L786NG_REG_PWM[i])
749                              & 0x0f) * 0x11;
750                 }
751
752
753                 /* Update the temperature sensors */
754                 for (i = 0; i < 2; i++) {
755                         for (j = 0; j < 3; j++) {
756                                 data->temp[i][j] = w83l786ng_read_value(client,
757                                     W83L786NG_REG_TEMP[i][j]);
758                         }
759                 }
760
761                 /* Update Smart Fan I/II tolerance */
762                 reg_tmp = w83l786ng_read_value(client, W83L786NG_REG_TOLERANCE);
763                 data->tolerance[0] = reg_tmp & 0x0f;
764                 data->tolerance[1] = (reg_tmp >> 4) & 0x0f;
765
766                 data->last_updated = jiffies;
767                 data->valid = 1;
768
769         }
770
771         mutex_unlock(&data->update_lock);
772
773         return data;
774 }
775
776 static int __init
777 sensors_w83l786ng_init(void)
778 {
779         return i2c_add_driver(&w83l786ng_driver);
780 }
781
782 static void __exit
783 sensors_w83l786ng_exit(void)
784 {
785         i2c_del_driver(&w83l786ng_driver);
786 }
787
788 MODULE_AUTHOR("Kevin Lo");
789 MODULE_DESCRIPTION("w83l786ng driver");
790 MODULE_LICENSE("GPL");
791
792 module_init(sensors_w83l786ng_init);
793 module_exit(sensors_w83l786ng_exit);