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