bna: fix interrupts storm caused by erroneous packets
[linux/fpc-iii.git] / drivers / hwmon / nct7802.c
blobfbfc02bb2cfa13c5bc22ece3a47a3cac1af07005
1 /*
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] = {
42 { 0, 0, 4, 0, 4 },
43 { 2, 0, 6, 2, 6 },
46 #define REG_BANK 0x00
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
52 #define REG_MODE 0x22
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
64 struct nct7802_data {
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;
73 int err;
75 *temp = 0;
77 mutex_lock(&data->access_lock);
78 err = regmap_read(data->regmap, reg_temp, &t1);
79 if (err < 0)
80 goto abort;
81 t1 <<= 8;
82 if (reg_temp_low) { /* 11 bit data */
83 err = regmap_read(data->regmap, reg_temp_low, &t2);
84 if (err < 0)
85 goto abort;
87 t1 |= t2 & 0xe0;
88 *temp = (s16)t1 / 32 * 125;
89 abort:
90 mutex_unlock(&data->access_lock);
91 return err;
94 static int nct7802_read_fan(struct nct7802_data *data, u8 reg_fan)
96 unsigned int f1, f2;
97 int ret;
99 mutex_lock(&data->access_lock);
100 ret = regmap_read(data->regmap, reg_fan, &f1);
101 if (ret < 0)
102 goto abort;
103 ret = regmap_read(data->regmap, REG_FANCOUNT_LOW, &f2);
104 if (ret < 0)
105 goto abort;
106 ret = (f1 << 5) | (f2 >> 3);
107 /* convert fan count to rpm */
108 if (ret == 0x1fff) /* maximum value, assume fan is stopped */
109 ret = 0;
110 else if (ret)
111 ret = DIV_ROUND_CLOSEST(1350000U, ret);
112 abort:
113 mutex_unlock(&data->access_lock);
114 return ret;
117 static int nct7802_read_fan_min(struct nct7802_data *data, u8 reg_fan_low,
118 u8 reg_fan_high)
120 unsigned int f1, f2;
121 int ret;
123 mutex_lock(&data->access_lock);
124 ret = regmap_read(data->regmap, reg_fan_low, &f1);
125 if (ret < 0)
126 goto abort;
127 ret = regmap_read(data->regmap, reg_fan_high, &f2);
128 if (ret < 0)
129 goto abort;
130 ret = f1 | ((f2 & 0xf8) << 5);
131 /* convert fan count to rpm */
132 if (ret == 0x1fff) /* maximum value, assume no limit */
133 ret = 0;
134 else if (ret)
135 ret = DIV_ROUND_CLOSEST(1350000U, ret);
136 abort:
137 mutex_unlock(&data->access_lock);
138 return ret;
141 static int nct7802_write_fan_min(struct nct7802_data *data, u8 reg_fan_low,
142 u8 reg_fan_high, unsigned int limit)
144 int err;
146 if (limit)
147 limit = DIV_ROUND_CLOSEST(1350000U, limit);
148 else
149 limit = 0x1fff;
150 limit = clamp_val(limit, 0, 0x1fff);
152 mutex_lock(&data->access_lock);
153 err = regmap_write(data->regmap, reg_fan_low, limit & 0xff);
154 if (err < 0)
155 goto abort;
157 err = regmap_write(data->regmap, reg_fan_high, (limit & 0x1f00) >> 5);
158 abort:
159 mutex_unlock(&data->access_lock);
160 return err;
163 static u8 nct7802_vmul[] = { 4, 2, 2, 2, 2 };
165 static int nct7802_read_voltage(struct nct7802_data *data, int nr, int index)
167 unsigned int v1, v2;
168 int ret;
170 mutex_lock(&data->access_lock);
171 if (index == 0) { /* voltage */
172 ret = regmap_read(data->regmap, REG_VOLTAGE[nr], &v1);
173 if (ret < 0)
174 goto abort;
175 ret = regmap_read(data->regmap, REG_VOLTAGE_LOW, &v2);
176 if (ret < 0)
177 goto abort;
178 ret = ((v1 << 2) | (v2 >> 6)) * nct7802_vmul[nr];
179 } else { /* limit */
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);
184 if (ret < 0)
185 goto abort;
186 ret = regmap_read(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
187 &v2);
188 if (ret < 0)
189 goto abort;
190 ret = (v1 | ((v2 << shift) & 0x300)) * nct7802_vmul[nr];
192 abort:
193 mutex_unlock(&data->access_lock);
194 return ret;
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];
201 int err;
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],
209 voltage & 0xff);
210 if (err < 0)
211 goto abort;
213 err = regmap_update_bits(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
214 0x0300 >> shift, (voltage & 0x0300) >> shift);
215 abort:
216 mutex_unlock(&data->access_lock);
217 return err;
220 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
221 char *buf)
223 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
224 struct nct7802_data *data = dev_get_drvdata(dev);
225 int voltage;
227 voltage = nct7802_read_voltage(data, sattr->nr, sattr->index);
228 if (voltage < 0)
229 return voltage;
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;
240 int nr = sattr->nr;
241 unsigned long val;
242 int err;
244 err = kstrtoul(buf, 10, &val);
245 if (err < 0)
246 return err;
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,
253 char *buf)
255 struct nct7802_data *data = dev_get_drvdata(dev);
256 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
257 int err, temp;
259 err = nct7802_read_temp(data, sattr->nr, sattr->index, &temp);
260 if (err < 0)
261 return err;
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);
271 int nr = sattr->nr;
272 long val;
273 int err;
275 err = kstrtol(buf, 10, &val);
276 if (err < 0)
277 return err;
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,
286 char *buf)
288 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
289 struct nct7802_data *data = dev_get_drvdata(dev);
290 int speed;
292 speed = nct7802_read_fan(data, sattr->index);
293 if (speed < 0)
294 return speed;
296 return sprintf(buf, "%d\n", speed);
299 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
300 char *buf)
302 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
303 struct nct7802_data *data = dev_get_drvdata(dev);
304 int speed;
306 speed = nct7802_read_fan_min(data, sattr->nr, sattr->index);
307 if (speed < 0)
308 return speed;
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);
318 unsigned long val;
319 int err;
321 err = kstrtoul(buf, 10, &val);
322 if (err < 0)
323 return err;
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,
330 char *buf)
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;
335 unsigned int val;
336 int ret;
338 ret = regmap_read(data->regmap, sattr->nr, &val);
339 if (ret < 0)
340 return ret;
342 return sprintf(buf, "%u\n", !!(val & (1 << bit)));
345 static ssize_t
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);
350 unsigned int regval;
351 int err;
353 err = regmap_read(data->regmap, sattr->nr, &regval);
354 if (err)
355 return err;
357 return sprintf(buf, "%u\n", !!(regval & (1 << sattr->index)));
360 static ssize_t
361 store_beep(struct device *dev, struct device_attribute *attr, const char *buf,
362 size_t count)
364 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
365 struct nct7802_data *data = dev_get_drvdata(dev);
366 unsigned long val;
367 int err;
369 err = kstrtoul(buf, 10, &val);
370 if (err < 0)
371 return err;
372 if (val > 1)
373 return -EINVAL;
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,
381 REG_TEMP_LSB);
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,
390 REG_TEMP_LSB);
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,
399 REG_TEMP_LSB);
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,
416 REG_TEMP_PECI_LSB);
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,
425 REG_TEMP_PECI_LSB);
427 static SENSOR_DEVICE_ATTR_2(temp1_min_alarm, S_IRUGO, show_alarm, NULL,
428 0x18, 0);
429 static SENSOR_DEVICE_ATTR_2(temp2_min_alarm, S_IRUGO, show_alarm, NULL,
430 0x18, 1);
431 static SENSOR_DEVICE_ATTR_2(temp3_min_alarm, S_IRUGO, show_alarm, NULL,
432 0x18, 2);
433 static SENSOR_DEVICE_ATTR_2(temp4_min_alarm, S_IRUGO, show_alarm, NULL,
434 0x18, 3);
435 static SENSOR_DEVICE_ATTR_2(temp5_min_alarm, S_IRUGO, show_alarm, NULL,
436 0x18, 4);
438 static SENSOR_DEVICE_ATTR_2(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
439 0x19, 0);
440 static SENSOR_DEVICE_ATTR_2(temp2_max_alarm, S_IRUGO, show_alarm, NULL,
441 0x19, 1);
442 static SENSOR_DEVICE_ATTR_2(temp3_max_alarm, S_IRUGO, show_alarm, NULL,
443 0x19, 2);
444 static SENSOR_DEVICE_ATTR_2(temp4_max_alarm, S_IRUGO, show_alarm, NULL,
445 0x19, 3);
446 static SENSOR_DEVICE_ATTR_2(temp5_max_alarm, S_IRUGO, show_alarm, NULL,
447 0x19, 4);
449 static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, show_alarm, NULL,
450 0x1b, 0);
451 static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm, S_IRUGO, show_alarm, NULL,
452 0x1b, 1);
453 static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm, S_IRUGO, show_alarm, NULL,
454 0x1b, 2);
455 static SENSOR_DEVICE_ATTR_2(temp4_crit_alarm, S_IRUGO, show_alarm, NULL,
456 0x1b, 3);
457 static SENSOR_DEVICE_ATTR_2(temp5_crit_alarm, S_IRUGO, show_alarm, NULL,
458 0x1b, 4);
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,
529 NULL
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);
537 unsigned int reg;
538 int err;
540 err = regmap_read(data->regmap, REG_MODE, &reg);
541 if (err < 0)
542 return 0;
544 if (index < 9 &&
545 (reg & 03) != 0x01 && (reg & 0x03) != 0x02) /* RD1 */
546 return 0;
547 if (index >= 9 && index < 18 &&
548 (reg & 0x0c) != 0x04 && (reg & 0x0c) != 0x08) /* RD2 */
549 return 0;
550 if (index >= 18 && index < 27 && (reg & 0x30) != 0x20) /* RD3 */
551 return 0;
552 if (index >= 27 && index < 35) /* local */
553 return attr->mode;
555 err = regmap_read(data->regmap, REG_PECI_ENABLE, &reg);
556 if (err < 0)
557 return 0;
559 if (index >= 35 && index < 43 && !(reg & 0x01)) /* PECI 0 */
560 return 0;
562 if (index >= 0x43 && (!(reg & 0x02))) /* PECI 1 */
563 return 0;
565 return attr->mode;
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,
575 0, 1);
576 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, store_in,
577 0, 2);
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,
580 0x5a, 3);
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,
586 2, 1);
587 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, store_in,
588 2, 2);
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,
591 0x5a, 0);
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,
595 3, 1);
596 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, store_in,
597 3, 2);
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,
600 0x5a, 1);
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,
604 4, 1);
605 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, store_in,
606 4, 2);
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,
609 0x5a, 2);
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,
638 NULL,
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);
646 unsigned int reg;
647 int err;
649 if (index < 6) /* VCC, VCORE */
650 return attr->mode;
652 err = regmap_read(data->regmap, REG_MODE, &reg);
653 if (err < 0)
654 return 0;
656 if (index >= 6 && index < 11 && (reg & 0x03) != 0x03) /* VSEN1 */
657 return 0;
658 if (index >= 11 && index < 17 && (reg & 0x0c) != 0x0c) /* VSEN2 */
659 return 0;
660 if (index >= 17 && (reg & 0x30) != 0x30) /* VSEN3 */
661 return 0;
663 return attr->mode;
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,
676 0x5b, 0);
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,
682 0x5b, 1);
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,
688 0x5b, 2);
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,
704 NULL
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 */
713 unsigned int reg;
714 int err;
716 err = regmap_read(data->regmap, REG_FAN_ENABLE, &reg);
717 if (err < 0 || !(reg & (1 << fan)))
718 return 0;
720 return attr->mode;
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[] = {
729 &nct7802_temp_group,
730 &nct7802_in_group,
731 &nct7802_fan_group,
732 NULL
735 static int nct7802_detect(struct i2c_client *client,
736 struct i2c_board_info *info)
738 int reg;
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);
745 if (reg != 0x00)
746 return -ENODEV;
748 reg = i2c_smbus_read_byte_data(client, REG_VENDOR_ID);
749 if (reg != 0x50)
750 return -ENODEV;
752 reg = i2c_smbus_read_byte_data(client, REG_CHIP_ID);
753 if (reg != 0xc3)
754 return -ENODEV;
756 reg = i2c_smbus_read_byte_data(client, REG_VERSION_ID);
757 if (reg < 0 || (reg & 0xf0) != 0x20)
758 return -ENODEV;
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))
763 return -ENODEV;
765 reg = i2c_smbus_read_byte_data(client, REG_TEMP_PECI_LSB);
766 if (reg < 0 || (reg & 0x3f))
767 return -ENODEV;
769 reg = i2c_smbus_read_byte_data(client, REG_VOLTAGE_LOW);
770 if (reg < 0 || (reg & 0x3f))
771 return -ENODEV;
773 strlcpy(info->type, "nct7802", I2C_NAME_SIZE);
774 return 0;
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 = {
783 .reg_bits = 8,
784 .val_bits = 8,
785 .cache_type = REGCACHE_RBTREE,
786 .volatile_reg = nct7802_regmap_is_volatile,
789 static int nct7802_init_chip(struct nct7802_data *data)
791 int err;
793 /* Enable ADC */
794 err = regmap_update_bits(data->regmap, REG_START, 0x01, 0x01);
795 if (err)
796 return err;
798 /* Enable local temperature sensor */
799 err = regmap_update_bits(data->regmap, REG_MODE, 0x40, 0x40);
800 if (err)
801 return err;
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;
813 int ret;
815 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
816 if (data == NULL)
817 return -ENOMEM;
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);
826 if (ret < 0)
827 return ret;
829 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
830 data,
831 nct7802_groups);
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[] = {
840 { "nct7802", 0 },
843 MODULE_DEVICE_TABLE(i2c, nct7802_idtable);
845 static struct i2c_driver nct7802_driver = {
846 .class = I2C_CLASS_HWMON,
847 .driver = {
848 .name = DRVNAME,
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");