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