2 * nct7802 - Driver for Nuvoton NCT7802Y
4 * Copyright (C) 2014 Guenter Roeck <linux@roeck-us.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/err.h>
20 #include <linux/i2c.h>
21 #include <linux/init.h>
22 #include <linux/hwmon.h>
23 #include <linux/hwmon-sysfs.h>
24 #include <linux/jiffies.h>
25 #include <linux/module.h>
26 #include <linux/mutex.h>
27 #include <linux/regmap.h>
28 #include <linux/slab.h>
30 #define DRVNAME "nct7802"
32 static const u8 REG_VOLTAGE
[5] = { 0x09, 0x0a, 0x0c, 0x0d, 0x0e };
34 static const u8 REG_VOLTAGE_LIMIT_LSB
[2][5] = {
35 { 0x40, 0x00, 0x42, 0x44, 0x46 },
36 { 0x3f, 0x00, 0x41, 0x43, 0x45 },
39 static const u8 REG_VOLTAGE_LIMIT_MSB
[5] = { 0x48, 0x00, 0x47, 0x47, 0x48 };
41 static const u8 REG_VOLTAGE_LIMIT_MSB_SHIFT
[2][5] = {
47 #define REG_TEMP_LSB 0x05
48 #define REG_TEMP_PECI_LSB 0x08
49 #define REG_VOLTAGE_LOW 0x0f
50 #define REG_FANCOUNT_LOW 0x13
51 #define REG_START 0x21
53 #define REG_PECI_ENABLE 0x23
54 #define REG_FAN_ENABLE 0x24
55 #define REG_VMON_ENABLE 0x25
56 #define REG_VENDOR_ID 0xfd
57 #define REG_CHIP_ID 0xfe
58 #define REG_VERSION_ID 0xff
61 * Data structures and manipulation thereof
65 struct regmap
*regmap
;
66 struct mutex access_lock
; /* for multi-byte read and write operations */
69 static int nct7802_read_temp(struct nct7802_data
*data
,
70 u8 reg_temp
, u8 reg_temp_low
, int *temp
)
72 unsigned int t1
, t2
= 0;
77 mutex_lock(&data
->access_lock
);
78 err
= regmap_read(data
->regmap
, reg_temp
, &t1
);
82 if (reg_temp_low
) { /* 11 bit data */
83 err
= regmap_read(data
->regmap
, reg_temp_low
, &t2
);
88 *temp
= (s16
)t1
/ 32 * 125;
90 mutex_unlock(&data
->access_lock
);
94 static int nct7802_read_fan(struct nct7802_data
*data
, u8 reg_fan
)
99 mutex_lock(&data
->access_lock
);
100 ret
= regmap_read(data
->regmap
, reg_fan
, &f1
);
103 ret
= regmap_read(data
->regmap
, REG_FANCOUNT_LOW
, &f2
);
106 ret
= (f1
<< 5) | (f2
>> 3);
107 /* convert fan count to rpm */
108 if (ret
== 0x1fff) /* maximum value, assume fan is stopped */
111 ret
= DIV_ROUND_CLOSEST(1350000U, ret
);
113 mutex_unlock(&data
->access_lock
);
117 static int nct7802_read_fan_min(struct nct7802_data
*data
, u8 reg_fan_low
,
123 mutex_lock(&data
->access_lock
);
124 ret
= regmap_read(data
->regmap
, reg_fan_low
, &f1
);
127 ret
= regmap_read(data
->regmap
, reg_fan_high
, &f2
);
130 ret
= f1
| ((f2
& 0xf8) << 5);
131 /* convert fan count to rpm */
132 if (ret
== 0x1fff) /* maximum value, assume no limit */
135 ret
= DIV_ROUND_CLOSEST(1350000U, ret
);
137 mutex_unlock(&data
->access_lock
);
141 static int nct7802_write_fan_min(struct nct7802_data
*data
, u8 reg_fan_low
,
142 u8 reg_fan_high
, unsigned int limit
)
147 limit
= DIV_ROUND_CLOSEST(1350000U, limit
);
150 limit
= clamp_val(limit
, 0, 0x1fff);
152 mutex_lock(&data
->access_lock
);
153 err
= regmap_write(data
->regmap
, reg_fan_low
, limit
& 0xff);
157 err
= regmap_write(data
->regmap
, reg_fan_high
, (limit
& 0x1f00) >> 5);
159 mutex_unlock(&data
->access_lock
);
163 static u8 nct7802_vmul
[] = { 4, 2, 2, 2, 2 };
165 static int nct7802_read_voltage(struct nct7802_data
*data
, int nr
, int index
)
170 mutex_lock(&data
->access_lock
);
171 if (index
== 0) { /* voltage */
172 ret
= regmap_read(data
->regmap
, REG_VOLTAGE
[nr
], &v1
);
175 ret
= regmap_read(data
->regmap
, REG_VOLTAGE_LOW
, &v2
);
178 ret
= ((v1
<< 2) | (v2
>> 6)) * nct7802_vmul
[nr
];
180 int shift
= 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT
[index
- 1][nr
];
182 ret
= regmap_read(data
->regmap
,
183 REG_VOLTAGE_LIMIT_LSB
[index
- 1][nr
], &v1
);
186 ret
= regmap_read(data
->regmap
, REG_VOLTAGE_LIMIT_MSB
[nr
],
190 ret
= (v1
| ((v2
<< shift
) & 0x300)) * nct7802_vmul
[nr
];
193 mutex_unlock(&data
->access_lock
);
197 static int nct7802_write_voltage(struct nct7802_data
*data
, int nr
, int index
,
198 unsigned long voltage
)
200 int shift
= 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT
[index
- 1][nr
];
203 voltage
= DIV_ROUND_CLOSEST(voltage
, nct7802_vmul
[nr
]);
204 voltage
= clamp_val(voltage
, 0, 0x3ff);
206 mutex_lock(&data
->access_lock
);
207 err
= regmap_write(data
->regmap
,
208 REG_VOLTAGE_LIMIT_LSB
[index
- 1][nr
],
213 err
= regmap_update_bits(data
->regmap
, REG_VOLTAGE_LIMIT_MSB
[nr
],
214 0x0300 >> shift
, (voltage
& 0x0300) >> shift
);
216 mutex_unlock(&data
->access_lock
);
220 static ssize_t
show_in(struct device
*dev
, struct device_attribute
*attr
,
223 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
224 struct nct7802_data
*data
= dev_get_drvdata(dev
);
227 voltage
= nct7802_read_voltage(data
, sattr
->nr
, sattr
->index
);
231 return sprintf(buf
, "%d\n", voltage
);
234 static ssize_t
store_in(struct device
*dev
, struct device_attribute
*attr
,
235 const char *buf
, size_t count
)
237 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
238 struct nct7802_data
*data
= dev_get_drvdata(dev
);
239 int index
= sattr
->index
;
244 err
= kstrtoul(buf
, 10, &val
);
248 err
= nct7802_write_voltage(data
, nr
, index
, val
);
249 return err
? : count
;
252 static ssize_t
show_temp(struct device
*dev
, struct device_attribute
*attr
,
255 struct nct7802_data
*data
= dev_get_drvdata(dev
);
256 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
259 err
= nct7802_read_temp(data
, sattr
->nr
, sattr
->index
, &temp
);
263 return sprintf(buf
, "%d\n", temp
);
266 static ssize_t
store_temp(struct device
*dev
, struct device_attribute
*attr
,
267 const char *buf
, size_t count
)
269 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
270 struct nct7802_data
*data
= dev_get_drvdata(dev
);
275 err
= kstrtol(buf
, 10, &val
);
279 val
= clamp_val(DIV_ROUND_CLOSEST(val
, 1000), -128, 127);
281 err
= regmap_write(data
->regmap
, nr
, val
& 0xff);
282 return err
? : count
;
285 static ssize_t
show_fan(struct device
*dev
, struct device_attribute
*attr
,
288 struct sensor_device_attribute
*sattr
= to_sensor_dev_attr(attr
);
289 struct nct7802_data
*data
= dev_get_drvdata(dev
);
292 speed
= nct7802_read_fan(data
, sattr
->index
);
296 return sprintf(buf
, "%d\n", speed
);
299 static ssize_t
show_fan_min(struct device
*dev
, struct device_attribute
*attr
,
302 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
303 struct nct7802_data
*data
= dev_get_drvdata(dev
);
306 speed
= nct7802_read_fan_min(data
, sattr
->nr
, sattr
->index
);
310 return sprintf(buf
, "%d\n", speed
);
313 static ssize_t
store_fan_min(struct device
*dev
, struct device_attribute
*attr
,
314 const char *buf
, size_t count
)
316 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
317 struct nct7802_data
*data
= dev_get_drvdata(dev
);
321 err
= kstrtoul(buf
, 10, &val
);
325 err
= nct7802_write_fan_min(data
, sattr
->nr
, sattr
->index
, val
);
326 return err
? : count
;
329 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
*attr
,
332 struct nct7802_data
*data
= dev_get_drvdata(dev
);
333 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
334 int bit
= sattr
->index
;
338 ret
= regmap_read(data
->regmap
, sattr
->nr
, &val
);
342 return sprintf(buf
, "%u\n", !!(val
& (1 << bit
)));
346 show_beep(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
348 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
349 struct nct7802_data
*data
= dev_get_drvdata(dev
);
353 err
= regmap_read(data
->regmap
, sattr
->nr
, ®val
);
357 return sprintf(buf
, "%u\n", !!(regval
& (1 << sattr
->index
)));
361 store_beep(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
364 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
365 struct nct7802_data
*data
= dev_get_drvdata(dev
);
369 err
= kstrtoul(buf
, 10, &val
);
375 err
= regmap_update_bits(data
->regmap
, sattr
->nr
, 1 << sattr
->index
,
376 val
? 1 << sattr
->index
: 0);
377 return err
? : count
;
380 static SENSOR_DEVICE_ATTR_2(temp1_input
, S_IRUGO
, show_temp
, NULL
, 0x01,
382 static SENSOR_DEVICE_ATTR_2(temp1_min
, S_IRUGO
| S_IWUSR
, show_temp
,
383 store_temp
, 0x31, 0);
384 static SENSOR_DEVICE_ATTR_2(temp1_max
, S_IRUGO
| S_IWUSR
, show_temp
,
385 store_temp
, 0x30, 0);
386 static SENSOR_DEVICE_ATTR_2(temp1_crit
, S_IRUGO
| S_IWUSR
, show_temp
,
387 store_temp
, 0x3a, 0);
389 static SENSOR_DEVICE_ATTR_2(temp2_input
, S_IRUGO
, show_temp
, NULL
, 0x02,
391 static SENSOR_DEVICE_ATTR_2(temp2_min
, S_IRUGO
| S_IWUSR
, show_temp
,
392 store_temp
, 0x33, 0);
393 static SENSOR_DEVICE_ATTR_2(temp2_max
, S_IRUGO
| S_IWUSR
, show_temp
,
394 store_temp
, 0x32, 0);
395 static SENSOR_DEVICE_ATTR_2(temp2_crit
, S_IRUGO
| S_IWUSR
, show_temp
,
396 store_temp
, 0x3b, 0);
398 static SENSOR_DEVICE_ATTR_2(temp3_input
, S_IRUGO
, show_temp
, NULL
, 0x03,
400 static SENSOR_DEVICE_ATTR_2(temp3_min
, S_IRUGO
| S_IWUSR
, show_temp
,
401 store_temp
, 0x35, 0);
402 static SENSOR_DEVICE_ATTR_2(temp3_max
, S_IRUGO
| S_IWUSR
, show_temp
,
403 store_temp
, 0x34, 0);
404 static SENSOR_DEVICE_ATTR_2(temp3_crit
, S_IRUGO
| S_IWUSR
, show_temp
,
405 store_temp
, 0x3c, 0);
407 static SENSOR_DEVICE_ATTR_2(temp4_input
, S_IRUGO
, show_temp
, NULL
, 0x04, 0);
408 static SENSOR_DEVICE_ATTR_2(temp4_min
, S_IRUGO
| S_IWUSR
, show_temp
,
409 store_temp
, 0x37, 0);
410 static SENSOR_DEVICE_ATTR_2(temp4_max
, S_IRUGO
| S_IWUSR
, show_temp
,
411 store_temp
, 0x36, 0);
412 static SENSOR_DEVICE_ATTR_2(temp4_crit
, S_IRUGO
| S_IWUSR
, show_temp
,
413 store_temp
, 0x3d, 0);
415 static SENSOR_DEVICE_ATTR_2(temp5_input
, S_IRUGO
, show_temp
, NULL
, 0x06,
417 static SENSOR_DEVICE_ATTR_2(temp5_min
, S_IRUGO
| S_IWUSR
, show_temp
,
418 store_temp
, 0x39, 0);
419 static SENSOR_DEVICE_ATTR_2(temp5_max
, S_IRUGO
| S_IWUSR
, show_temp
,
420 store_temp
, 0x38, 0);
421 static SENSOR_DEVICE_ATTR_2(temp5_crit
, S_IRUGO
| S_IWUSR
, show_temp
,
422 store_temp
, 0x3e, 0);
424 static SENSOR_DEVICE_ATTR_2(temp6_input
, S_IRUGO
, show_temp
, NULL
, 0x07,
427 static SENSOR_DEVICE_ATTR_2(temp1_min_alarm
, S_IRUGO
, show_alarm
, NULL
,
429 static SENSOR_DEVICE_ATTR_2(temp2_min_alarm
, S_IRUGO
, show_alarm
, NULL
,
431 static SENSOR_DEVICE_ATTR_2(temp3_min_alarm
, S_IRUGO
, show_alarm
, NULL
,
433 static SENSOR_DEVICE_ATTR_2(temp4_min_alarm
, S_IRUGO
, show_alarm
, NULL
,
435 static SENSOR_DEVICE_ATTR_2(temp5_min_alarm
, S_IRUGO
, show_alarm
, NULL
,
438 static SENSOR_DEVICE_ATTR_2(temp1_max_alarm
, S_IRUGO
, show_alarm
, NULL
,
440 static SENSOR_DEVICE_ATTR_2(temp2_max_alarm
, S_IRUGO
, show_alarm
, NULL
,
442 static SENSOR_DEVICE_ATTR_2(temp3_max_alarm
, S_IRUGO
, show_alarm
, NULL
,
444 static SENSOR_DEVICE_ATTR_2(temp4_max_alarm
, S_IRUGO
, show_alarm
, NULL
,
446 static SENSOR_DEVICE_ATTR_2(temp5_max_alarm
, S_IRUGO
, show_alarm
, NULL
,
449 static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm
, S_IRUGO
, show_alarm
, NULL
,
451 static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm
, S_IRUGO
, show_alarm
, NULL
,
453 static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm
, S_IRUGO
, show_alarm
, NULL
,
455 static SENSOR_DEVICE_ATTR_2(temp4_crit_alarm
, S_IRUGO
, show_alarm
, NULL
,
457 static SENSOR_DEVICE_ATTR_2(temp5_crit_alarm
, S_IRUGO
, show_alarm
, NULL
,
460 static SENSOR_DEVICE_ATTR_2(temp1_fault
, S_IRUGO
, show_alarm
, NULL
, 0x17, 0);
461 static SENSOR_DEVICE_ATTR_2(temp2_fault
, S_IRUGO
, show_alarm
, NULL
, 0x17, 1);
462 static SENSOR_DEVICE_ATTR_2(temp3_fault
, S_IRUGO
, show_alarm
, NULL
, 0x17, 2);
464 static SENSOR_DEVICE_ATTR_2(temp1_beep
, S_IRUGO
| S_IWUSR
, show_beep
,
465 store_beep
, 0x5c, 0);
466 static SENSOR_DEVICE_ATTR_2(temp2_beep
, S_IRUGO
| S_IWUSR
, show_beep
,
467 store_beep
, 0x5c, 1);
468 static SENSOR_DEVICE_ATTR_2(temp3_beep
, S_IRUGO
| S_IWUSR
, show_beep
,
469 store_beep
, 0x5c, 2);
470 static SENSOR_DEVICE_ATTR_2(temp4_beep
, S_IRUGO
| S_IWUSR
, show_beep
,
471 store_beep
, 0x5c, 3);
472 static SENSOR_DEVICE_ATTR_2(temp5_beep
, S_IRUGO
| S_IWUSR
, show_beep
,
473 store_beep
, 0x5c, 4);
474 static SENSOR_DEVICE_ATTR_2(temp6_beep
, S_IRUGO
| S_IWUSR
, show_beep
,
475 store_beep
, 0x5c, 5);
477 static struct attribute
*nct7802_temp_attrs
[] = {
478 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
479 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
480 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
481 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
482 &sensor_dev_attr_temp1_min_alarm
.dev_attr
.attr
,
483 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
484 &sensor_dev_attr_temp1_crit_alarm
.dev_attr
.attr
,
485 &sensor_dev_attr_temp1_fault
.dev_attr
.attr
,
486 &sensor_dev_attr_temp1_beep
.dev_attr
.attr
,
488 &sensor_dev_attr_temp2_input
.dev_attr
.attr
, /* 9 */
489 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
490 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
491 &sensor_dev_attr_temp2_crit
.dev_attr
.attr
,
492 &sensor_dev_attr_temp2_min_alarm
.dev_attr
.attr
,
493 &sensor_dev_attr_temp2_max_alarm
.dev_attr
.attr
,
494 &sensor_dev_attr_temp2_crit_alarm
.dev_attr
.attr
,
495 &sensor_dev_attr_temp2_fault
.dev_attr
.attr
,
496 &sensor_dev_attr_temp2_beep
.dev_attr
.attr
,
498 &sensor_dev_attr_temp3_input
.dev_attr
.attr
, /* 18 */
499 &sensor_dev_attr_temp3_min
.dev_attr
.attr
,
500 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
501 &sensor_dev_attr_temp3_crit
.dev_attr
.attr
,
502 &sensor_dev_attr_temp3_min_alarm
.dev_attr
.attr
,
503 &sensor_dev_attr_temp3_max_alarm
.dev_attr
.attr
,
504 &sensor_dev_attr_temp3_crit_alarm
.dev_attr
.attr
,
505 &sensor_dev_attr_temp3_fault
.dev_attr
.attr
,
506 &sensor_dev_attr_temp3_beep
.dev_attr
.attr
,
508 &sensor_dev_attr_temp4_input
.dev_attr
.attr
, /* 27 */
509 &sensor_dev_attr_temp4_min
.dev_attr
.attr
,
510 &sensor_dev_attr_temp4_max
.dev_attr
.attr
,
511 &sensor_dev_attr_temp4_crit
.dev_attr
.attr
,
512 &sensor_dev_attr_temp4_min_alarm
.dev_attr
.attr
,
513 &sensor_dev_attr_temp4_max_alarm
.dev_attr
.attr
,
514 &sensor_dev_attr_temp4_crit_alarm
.dev_attr
.attr
,
515 &sensor_dev_attr_temp4_beep
.dev_attr
.attr
,
517 &sensor_dev_attr_temp5_input
.dev_attr
.attr
, /* 35 */
518 &sensor_dev_attr_temp5_min
.dev_attr
.attr
,
519 &sensor_dev_attr_temp5_max
.dev_attr
.attr
,
520 &sensor_dev_attr_temp5_crit
.dev_attr
.attr
,
521 &sensor_dev_attr_temp5_min_alarm
.dev_attr
.attr
,
522 &sensor_dev_attr_temp5_max_alarm
.dev_attr
.attr
,
523 &sensor_dev_attr_temp5_crit_alarm
.dev_attr
.attr
,
524 &sensor_dev_attr_temp5_beep
.dev_attr
.attr
,
526 &sensor_dev_attr_temp6_input
.dev_attr
.attr
, /* 43 */
527 &sensor_dev_attr_temp6_beep
.dev_attr
.attr
,
532 static umode_t
nct7802_temp_is_visible(struct kobject
*kobj
,
533 struct attribute
*attr
, int index
)
535 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
536 struct nct7802_data
*data
= dev_get_drvdata(dev
);
540 err
= regmap_read(data
->regmap
, REG_MODE
, ®
);
545 (reg
& 03) != 0x01 && (reg
& 0x03) != 0x02) /* RD1 */
547 if (index
>= 9 && index
< 18 &&
548 (reg
& 0x0c) != 0x04 && (reg
& 0x0c) != 0x08) /* RD2 */
550 if (index
>= 18 && index
< 27 && (reg
& 0x30) != 0x20) /* RD3 */
552 if (index
>= 27 && index
< 35) /* local */
555 err
= regmap_read(data
->regmap
, REG_PECI_ENABLE
, ®
);
559 if (index
>= 35 && index
< 43 && !(reg
& 0x01)) /* PECI 0 */
562 if (index
>= 0x43 && (!(reg
& 0x02))) /* PECI 1 */
568 static struct attribute_group nct7802_temp_group
= {
569 .attrs
= nct7802_temp_attrs
,
570 .is_visible
= nct7802_temp_is_visible
,
573 static SENSOR_DEVICE_ATTR_2(in0_input
, S_IRUGO
, show_in
, NULL
, 0, 0);
574 static SENSOR_DEVICE_ATTR_2(in0_min
, S_IRUGO
| S_IWUSR
, show_in
, store_in
,
576 static SENSOR_DEVICE_ATTR_2(in0_max
, S_IRUGO
| S_IWUSR
, show_in
, store_in
,
578 static SENSOR_DEVICE_ATTR_2(in0_alarm
, S_IRUGO
, show_alarm
, NULL
, 0x1e, 3);
579 static SENSOR_DEVICE_ATTR_2(in0_beep
, S_IRUGO
| S_IWUSR
, show_beep
, store_beep
,
582 static SENSOR_DEVICE_ATTR_2(in1_input
, S_IRUGO
, show_in
, NULL
, 1, 0);
584 static SENSOR_DEVICE_ATTR_2(in2_input
, S_IRUGO
, show_in
, NULL
, 2, 0);
585 static SENSOR_DEVICE_ATTR_2(in2_min
, S_IRUGO
| S_IWUSR
, show_in
, store_in
,
587 static SENSOR_DEVICE_ATTR_2(in2_max
, S_IRUGO
| S_IWUSR
, show_in
, store_in
,
589 static SENSOR_DEVICE_ATTR_2(in2_alarm
, S_IRUGO
, show_alarm
, NULL
, 0x1e, 0);
590 static SENSOR_DEVICE_ATTR_2(in2_beep
, S_IRUGO
| S_IWUSR
, show_beep
, store_beep
,
593 static SENSOR_DEVICE_ATTR_2(in3_input
, S_IRUGO
, show_in
, NULL
, 3, 0);
594 static SENSOR_DEVICE_ATTR_2(in3_min
, S_IRUGO
| S_IWUSR
, show_in
, store_in
,
596 static SENSOR_DEVICE_ATTR_2(in3_max
, S_IRUGO
| S_IWUSR
, show_in
, store_in
,
598 static SENSOR_DEVICE_ATTR_2(in3_alarm
, S_IRUGO
, show_alarm
, NULL
, 0x1e, 1);
599 static SENSOR_DEVICE_ATTR_2(in3_beep
, S_IRUGO
| S_IWUSR
, show_beep
, store_beep
,
602 static SENSOR_DEVICE_ATTR_2(in4_input
, S_IRUGO
, show_in
, NULL
, 4, 0);
603 static SENSOR_DEVICE_ATTR_2(in4_min
, S_IRUGO
| S_IWUSR
, show_in
, store_in
,
605 static SENSOR_DEVICE_ATTR_2(in4_max
, S_IRUGO
| S_IWUSR
, show_in
, store_in
,
607 static SENSOR_DEVICE_ATTR_2(in4_alarm
, S_IRUGO
, show_alarm
, NULL
, 0x1e, 2);
608 static SENSOR_DEVICE_ATTR_2(in4_beep
, S_IRUGO
| S_IWUSR
, show_beep
, store_beep
,
611 static struct attribute
*nct7802_in_attrs
[] = {
612 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
613 &sensor_dev_attr_in0_min
.dev_attr
.attr
,
614 &sensor_dev_attr_in0_max
.dev_attr
.attr
,
615 &sensor_dev_attr_in0_alarm
.dev_attr
.attr
,
616 &sensor_dev_attr_in0_beep
.dev_attr
.attr
,
618 &sensor_dev_attr_in1_input
.dev_attr
.attr
, /* 5 */
620 &sensor_dev_attr_in2_input
.dev_attr
.attr
, /* 6 */
621 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
622 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
623 &sensor_dev_attr_in2_alarm
.dev_attr
.attr
,
624 &sensor_dev_attr_in2_beep
.dev_attr
.attr
,
626 &sensor_dev_attr_in3_input
.dev_attr
.attr
, /* 11 */
627 &sensor_dev_attr_in3_min
.dev_attr
.attr
,
628 &sensor_dev_attr_in3_max
.dev_attr
.attr
,
629 &sensor_dev_attr_in3_alarm
.dev_attr
.attr
,
630 &sensor_dev_attr_in3_beep
.dev_attr
.attr
,
632 &sensor_dev_attr_in4_input
.dev_attr
.attr
, /* 17 */
633 &sensor_dev_attr_in4_min
.dev_attr
.attr
,
634 &sensor_dev_attr_in4_max
.dev_attr
.attr
,
635 &sensor_dev_attr_in4_alarm
.dev_attr
.attr
,
636 &sensor_dev_attr_in4_beep
.dev_attr
.attr
,
641 static umode_t
nct7802_in_is_visible(struct kobject
*kobj
,
642 struct attribute
*attr
, int index
)
644 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
645 struct nct7802_data
*data
= dev_get_drvdata(dev
);
649 if (index
< 6) /* VCC, VCORE */
652 err
= regmap_read(data
->regmap
, REG_MODE
, ®
);
656 if (index
>= 6 && index
< 11 && (reg
& 0x03) != 0x03) /* VSEN1 */
658 if (index
>= 11 && index
< 17 && (reg
& 0x0c) != 0x0c) /* VSEN2 */
660 if (index
>= 17 && (reg
& 0x30) != 0x30) /* VSEN3 */
666 static struct attribute_group nct7802_in_group
= {
667 .attrs
= nct7802_in_attrs
,
668 .is_visible
= nct7802_in_is_visible
,
671 static SENSOR_DEVICE_ATTR(fan1_input
, S_IRUGO
, show_fan
, NULL
, 0x10);
672 static SENSOR_DEVICE_ATTR_2(fan1_min
, S_IRUGO
| S_IWUSR
, show_fan_min
,
673 store_fan_min
, 0x49, 0x4c);
674 static SENSOR_DEVICE_ATTR_2(fan1_alarm
, S_IRUGO
, show_alarm
, NULL
, 0x1a, 0);
675 static SENSOR_DEVICE_ATTR_2(fan1_beep
, S_IRUGO
| S_IWUSR
, show_beep
, store_beep
,
677 static SENSOR_DEVICE_ATTR(fan2_input
, S_IRUGO
, show_fan
, NULL
, 0x11);
678 static SENSOR_DEVICE_ATTR_2(fan2_min
, S_IRUGO
| S_IWUSR
, show_fan_min
,
679 store_fan_min
, 0x4a, 0x4d);
680 static SENSOR_DEVICE_ATTR_2(fan2_alarm
, S_IRUGO
, show_alarm
, NULL
, 0x1a, 1);
681 static SENSOR_DEVICE_ATTR_2(fan2_beep
, S_IRUGO
| S_IWUSR
, show_beep
, store_beep
,
683 static SENSOR_DEVICE_ATTR(fan3_input
, S_IRUGO
, show_fan
, NULL
, 0x12);
684 static SENSOR_DEVICE_ATTR_2(fan3_min
, S_IRUGO
| S_IWUSR
, show_fan_min
,
685 store_fan_min
, 0x4b, 0x4e);
686 static SENSOR_DEVICE_ATTR_2(fan3_alarm
, S_IRUGO
, show_alarm
, NULL
, 0x1a, 2);
687 static SENSOR_DEVICE_ATTR_2(fan3_beep
, S_IRUGO
| S_IWUSR
, show_beep
, store_beep
,
690 static struct attribute
*nct7802_fan_attrs
[] = {
691 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
692 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
693 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
694 &sensor_dev_attr_fan1_beep
.dev_attr
.attr
,
695 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
696 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
697 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
698 &sensor_dev_attr_fan2_beep
.dev_attr
.attr
,
699 &sensor_dev_attr_fan3_input
.dev_attr
.attr
,
700 &sensor_dev_attr_fan3_min
.dev_attr
.attr
,
701 &sensor_dev_attr_fan3_alarm
.dev_attr
.attr
,
702 &sensor_dev_attr_fan3_beep
.dev_attr
.attr
,
707 static umode_t
nct7802_fan_is_visible(struct kobject
*kobj
,
708 struct attribute
*attr
, int index
)
710 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
711 struct nct7802_data
*data
= dev_get_drvdata(dev
);
712 int fan
= index
/ 4; /* 4 attributes per fan */
716 err
= regmap_read(data
->regmap
, REG_FAN_ENABLE
, ®
);
717 if (err
< 0 || !(reg
& (1 << fan
)))
723 static struct attribute_group nct7802_fan_group
= {
724 .attrs
= nct7802_fan_attrs
,
725 .is_visible
= nct7802_fan_is_visible
,
728 static const struct attribute_group
*nct7802_groups
[] = {
735 static int nct7802_detect(struct i2c_client
*client
,
736 struct i2c_board_info
*info
)
741 * Chip identification registers are only available in bank 0,
742 * so only attempt chip detection if bank 0 is selected
744 reg
= i2c_smbus_read_byte_data(client
, REG_BANK
);
748 reg
= i2c_smbus_read_byte_data(client
, REG_VENDOR_ID
);
752 reg
= i2c_smbus_read_byte_data(client
, REG_CHIP_ID
);
756 reg
= i2c_smbus_read_byte_data(client
, REG_VERSION_ID
);
757 if (reg
< 0 || (reg
& 0xf0) != 0x20)
760 /* Also validate lower bits of voltage and temperature registers */
761 reg
= i2c_smbus_read_byte_data(client
, REG_TEMP_LSB
);
762 if (reg
< 0 || (reg
& 0x1f))
765 reg
= i2c_smbus_read_byte_data(client
, REG_TEMP_PECI_LSB
);
766 if (reg
< 0 || (reg
& 0x3f))
769 reg
= i2c_smbus_read_byte_data(client
, REG_VOLTAGE_LOW
);
770 if (reg
< 0 || (reg
& 0x3f))
773 strlcpy(info
->type
, "nct7802", I2C_NAME_SIZE
);
777 static bool nct7802_regmap_is_volatile(struct device
*dev
, unsigned int reg
)
779 return reg
!= REG_BANK
&& reg
<= 0x20;
782 static const struct regmap_config nct7802_regmap_config
= {
785 .cache_type
= REGCACHE_RBTREE
,
786 .volatile_reg
= nct7802_regmap_is_volatile
,
789 static int nct7802_init_chip(struct nct7802_data
*data
)
794 err
= regmap_update_bits(data
->regmap
, REG_START
, 0x01, 0x01);
798 /* Enable local temperature sensor */
799 err
= regmap_update_bits(data
->regmap
, REG_MODE
, 0x40, 0x40);
803 /* Enable Vcore and VCC voltage monitoring */
804 return regmap_update_bits(data
->regmap
, REG_VMON_ENABLE
, 0x03, 0x03);
807 static int nct7802_probe(struct i2c_client
*client
,
808 const struct i2c_device_id
*id
)
810 struct device
*dev
= &client
->dev
;
811 struct nct7802_data
*data
;
812 struct device
*hwmon_dev
;
815 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
819 data
->regmap
= devm_regmap_init_i2c(client
, &nct7802_regmap_config
);
820 if (IS_ERR(data
->regmap
))
821 return PTR_ERR(data
->regmap
);
823 mutex_init(&data
->access_lock
);
825 ret
= nct7802_init_chip(data
);
829 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
832 return PTR_ERR_OR_ZERO(hwmon_dev
);
835 static const unsigned short nct7802_address_list
[] = {
836 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
839 static const struct i2c_device_id nct7802_idtable
[] = {
843 MODULE_DEVICE_TABLE(i2c
, nct7802_idtable
);
845 static struct i2c_driver nct7802_driver
= {
846 .class = I2C_CLASS_HWMON
,
850 .detect
= nct7802_detect
,
851 .probe
= nct7802_probe
,
852 .id_table
= nct7802_idtable
,
853 .address_list
= nct7802_address_list
,
856 module_i2c_driver(nct7802_driver
);
858 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
859 MODULE_DESCRIPTION("NCT7802Y Hardware Monitoring Driver");
860 MODULE_LICENSE("GPL v2");