interconnect: qcom: Fix Kconfig indentation
[linux/fpc-iii.git] / drivers / hwmon / nct7802.c
blobf3dd2a17bd426244d72d26a8d027fa24523cbb00
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * nct7802 - Driver for Nuvoton NCT7802Y
5 * Copyright (C) 2014 Guenter Roeck <linux@roeck-us.net>
6 */
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/err.h>
11 #include <linux/i2c.h>
12 #include <linux/init.h>
13 #include <linux/hwmon.h>
14 #include <linux/hwmon-sysfs.h>
15 #include <linux/jiffies.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
21 #define DRVNAME "nct7802"
23 static const u8 REG_VOLTAGE[5] = { 0x09, 0x0a, 0x0c, 0x0d, 0x0e };
25 static const u8 REG_VOLTAGE_LIMIT_LSB[2][5] = {
26 { 0x40, 0x00, 0x42, 0x44, 0x46 },
27 { 0x3f, 0x00, 0x41, 0x43, 0x45 },
30 static const u8 REG_VOLTAGE_LIMIT_MSB[5] = { 0x48, 0x00, 0x47, 0x47, 0x48 };
32 static const u8 REG_VOLTAGE_LIMIT_MSB_SHIFT[2][5] = {
33 { 0, 0, 4, 0, 4 },
34 { 2, 0, 6, 2, 6 },
37 #define REG_BANK 0x00
38 #define REG_TEMP_LSB 0x05
39 #define REG_TEMP_PECI_LSB 0x08
40 #define REG_VOLTAGE_LOW 0x0f
41 #define REG_FANCOUNT_LOW 0x13
42 #define REG_START 0x21
43 #define REG_MODE 0x22 /* 7.2.32 Mode Selection Register */
44 #define REG_PECI_ENABLE 0x23
45 #define REG_FAN_ENABLE 0x24
46 #define REG_VMON_ENABLE 0x25
47 #define REG_PWM(x) (0x60 + (x))
48 #define REG_SMARTFAN_EN(x) (0x64 + (x) / 2)
49 #define SMARTFAN_EN_SHIFT(x) ((x) % 2 * 4)
50 #define REG_VENDOR_ID 0xfd
51 #define REG_CHIP_ID 0xfe
52 #define REG_VERSION_ID 0xff
55 * Data structures and manipulation thereof
58 struct nct7802_data {
59 struct regmap *regmap;
60 struct mutex access_lock; /* for multi-byte read and write operations */
63 static ssize_t temp_type_show(struct device *dev,
64 struct device_attribute *attr, char *buf)
66 struct nct7802_data *data = dev_get_drvdata(dev);
67 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
68 unsigned int mode;
69 int ret;
71 ret = regmap_read(data->regmap, REG_MODE, &mode);
72 if (ret < 0)
73 return ret;
75 return sprintf(buf, "%u\n", (mode >> (2 * sattr->index) & 3) + 2);
78 static ssize_t temp_type_store(struct device *dev,
79 struct device_attribute *attr, const char *buf,
80 size_t count)
82 struct nct7802_data *data = dev_get_drvdata(dev);
83 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
84 unsigned int type;
85 int err;
87 err = kstrtouint(buf, 0, &type);
88 if (err < 0)
89 return err;
90 if (sattr->index == 2 && type != 4) /* RD3 */
91 return -EINVAL;
92 if (type < 3 || type > 4)
93 return -EINVAL;
94 err = regmap_update_bits(data->regmap, REG_MODE,
95 3 << 2 * sattr->index, (type - 2) << 2 * sattr->index);
96 return err ? : count;
99 static ssize_t pwm_mode_show(struct device *dev,
100 struct device_attribute *attr, char *buf)
102 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
103 struct nct7802_data *data = dev_get_drvdata(dev);
104 unsigned int regval;
105 int ret;
107 if (sattr->index > 1)
108 return sprintf(buf, "1\n");
110 ret = regmap_read(data->regmap, 0x5E, &regval);
111 if (ret < 0)
112 return ret;
114 return sprintf(buf, "%u\n", !(regval & (1 << sattr->index)));
117 static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
118 char *buf)
120 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
121 struct nct7802_data *data = dev_get_drvdata(dev);
122 unsigned int val;
123 int ret;
125 if (!attr->index)
126 return sprintf(buf, "255\n");
128 ret = regmap_read(data->regmap, attr->index, &val);
129 if (ret < 0)
130 return ret;
132 return sprintf(buf, "%d\n", val);
135 static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
136 const char *buf, size_t count)
138 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
139 struct nct7802_data *data = dev_get_drvdata(dev);
140 int err;
141 u8 val;
143 err = kstrtou8(buf, 0, &val);
144 if (err < 0)
145 return err;
147 err = regmap_write(data->regmap, attr->index, val);
148 return err ? : count;
151 static ssize_t pwm_enable_show(struct device *dev,
152 struct device_attribute *attr, char *buf)
154 struct nct7802_data *data = dev_get_drvdata(dev);
155 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
156 unsigned int reg, enabled;
157 int ret;
159 ret = regmap_read(data->regmap, REG_SMARTFAN_EN(sattr->index), &reg);
160 if (ret < 0)
161 return ret;
162 enabled = reg >> SMARTFAN_EN_SHIFT(sattr->index) & 1;
163 return sprintf(buf, "%u\n", enabled + 1);
166 static ssize_t pwm_enable_store(struct device *dev,
167 struct device_attribute *attr,
168 const char *buf, size_t count)
170 struct nct7802_data *data = dev_get_drvdata(dev);
171 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
172 u8 val;
173 int ret;
175 ret = kstrtou8(buf, 0, &val);
176 if (ret < 0)
177 return ret;
178 if (val < 1 || val > 2)
179 return -EINVAL;
180 ret = regmap_update_bits(data->regmap, REG_SMARTFAN_EN(sattr->index),
181 1 << SMARTFAN_EN_SHIFT(sattr->index),
182 (val - 1) << SMARTFAN_EN_SHIFT(sattr->index));
183 return ret ? : count;
186 static int nct7802_read_temp(struct nct7802_data *data,
187 u8 reg_temp, u8 reg_temp_low, int *temp)
189 unsigned int t1, t2 = 0;
190 int err;
192 *temp = 0;
194 mutex_lock(&data->access_lock);
195 err = regmap_read(data->regmap, reg_temp, &t1);
196 if (err < 0)
197 goto abort;
198 t1 <<= 8;
199 if (reg_temp_low) { /* 11 bit data */
200 err = regmap_read(data->regmap, reg_temp_low, &t2);
201 if (err < 0)
202 goto abort;
204 t1 |= t2 & 0xe0;
205 *temp = (s16)t1 / 32 * 125;
206 abort:
207 mutex_unlock(&data->access_lock);
208 return err;
211 static int nct7802_read_fan(struct nct7802_data *data, u8 reg_fan)
213 unsigned int f1, f2;
214 int ret;
216 mutex_lock(&data->access_lock);
217 ret = regmap_read(data->regmap, reg_fan, &f1);
218 if (ret < 0)
219 goto abort;
220 ret = regmap_read(data->regmap, REG_FANCOUNT_LOW, &f2);
221 if (ret < 0)
222 goto abort;
223 ret = (f1 << 5) | (f2 >> 3);
224 /* convert fan count to rpm */
225 if (ret == 0x1fff) /* maximum value, assume fan is stopped */
226 ret = 0;
227 else if (ret)
228 ret = DIV_ROUND_CLOSEST(1350000U, ret);
229 abort:
230 mutex_unlock(&data->access_lock);
231 return ret;
234 static int nct7802_read_fan_min(struct nct7802_data *data, u8 reg_fan_low,
235 u8 reg_fan_high)
237 unsigned int f1, f2;
238 int ret;
240 mutex_lock(&data->access_lock);
241 ret = regmap_read(data->regmap, reg_fan_low, &f1);
242 if (ret < 0)
243 goto abort;
244 ret = regmap_read(data->regmap, reg_fan_high, &f2);
245 if (ret < 0)
246 goto abort;
247 ret = f1 | ((f2 & 0xf8) << 5);
248 /* convert fan count to rpm */
249 if (ret == 0x1fff) /* maximum value, assume no limit */
250 ret = 0;
251 else if (ret)
252 ret = DIV_ROUND_CLOSEST(1350000U, ret);
253 else
254 ret = 1350000U;
255 abort:
256 mutex_unlock(&data->access_lock);
257 return ret;
260 static int nct7802_write_fan_min(struct nct7802_data *data, u8 reg_fan_low,
261 u8 reg_fan_high, unsigned long limit)
263 int err;
265 if (limit)
266 limit = DIV_ROUND_CLOSEST(1350000U, limit);
267 else
268 limit = 0x1fff;
269 limit = clamp_val(limit, 0, 0x1fff);
271 mutex_lock(&data->access_lock);
272 err = regmap_write(data->regmap, reg_fan_low, limit & 0xff);
273 if (err < 0)
274 goto abort;
276 err = regmap_write(data->regmap, reg_fan_high, (limit & 0x1f00) >> 5);
277 abort:
278 mutex_unlock(&data->access_lock);
279 return err;
282 static u8 nct7802_vmul[] = { 4, 2, 2, 2, 2 };
284 static int nct7802_read_voltage(struct nct7802_data *data, int nr, int index)
286 unsigned int v1, v2;
287 int ret;
289 mutex_lock(&data->access_lock);
290 if (index == 0) { /* voltage */
291 ret = regmap_read(data->regmap, REG_VOLTAGE[nr], &v1);
292 if (ret < 0)
293 goto abort;
294 ret = regmap_read(data->regmap, REG_VOLTAGE_LOW, &v2);
295 if (ret < 0)
296 goto abort;
297 ret = ((v1 << 2) | (v2 >> 6)) * nct7802_vmul[nr];
298 } else { /* limit */
299 int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
301 ret = regmap_read(data->regmap,
302 REG_VOLTAGE_LIMIT_LSB[index - 1][nr], &v1);
303 if (ret < 0)
304 goto abort;
305 ret = regmap_read(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
306 &v2);
307 if (ret < 0)
308 goto abort;
309 ret = (v1 | ((v2 << shift) & 0x300)) * nct7802_vmul[nr];
311 abort:
312 mutex_unlock(&data->access_lock);
313 return ret;
316 static int nct7802_write_voltage(struct nct7802_data *data, int nr, int index,
317 unsigned long voltage)
319 int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
320 int err;
322 voltage = clamp_val(voltage, 0, 0x3ff * nct7802_vmul[nr]);
323 voltage = DIV_ROUND_CLOSEST(voltage, nct7802_vmul[nr]);
325 mutex_lock(&data->access_lock);
326 err = regmap_write(data->regmap,
327 REG_VOLTAGE_LIMIT_LSB[index - 1][nr],
328 voltage & 0xff);
329 if (err < 0)
330 goto abort;
332 err = regmap_update_bits(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
333 0x0300 >> shift, (voltage & 0x0300) >> shift);
334 abort:
335 mutex_unlock(&data->access_lock);
336 return err;
339 static ssize_t in_show(struct device *dev, struct device_attribute *attr,
340 char *buf)
342 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
343 struct nct7802_data *data = dev_get_drvdata(dev);
344 int voltage;
346 voltage = nct7802_read_voltage(data, sattr->nr, sattr->index);
347 if (voltage < 0)
348 return voltage;
350 return sprintf(buf, "%d\n", voltage);
353 static ssize_t in_store(struct device *dev, struct device_attribute *attr,
354 const char *buf, size_t count)
356 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
357 struct nct7802_data *data = dev_get_drvdata(dev);
358 int index = sattr->index;
359 int nr = sattr->nr;
360 unsigned long val;
361 int err;
363 err = kstrtoul(buf, 10, &val);
364 if (err < 0)
365 return err;
367 err = nct7802_write_voltage(data, nr, index, val);
368 return err ? : count;
371 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
372 char *buf)
374 struct nct7802_data *data = dev_get_drvdata(dev);
375 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
376 int err, temp;
378 err = nct7802_read_temp(data, sattr->nr, sattr->index, &temp);
379 if (err < 0)
380 return err;
382 return sprintf(buf, "%d\n", temp);
385 static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
386 const char *buf, size_t count)
388 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
389 struct nct7802_data *data = dev_get_drvdata(dev);
390 int nr = sattr->nr;
391 long val;
392 int err;
394 err = kstrtol(buf, 10, &val);
395 if (err < 0)
396 return err;
398 val = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
400 err = regmap_write(data->regmap, nr, val & 0xff);
401 return err ? : count;
404 static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
405 char *buf)
407 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
408 struct nct7802_data *data = dev_get_drvdata(dev);
409 int speed;
411 speed = nct7802_read_fan(data, sattr->index);
412 if (speed < 0)
413 return speed;
415 return sprintf(buf, "%d\n", speed);
418 static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
419 char *buf)
421 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
422 struct nct7802_data *data = dev_get_drvdata(dev);
423 int speed;
425 speed = nct7802_read_fan_min(data, sattr->nr, sattr->index);
426 if (speed < 0)
427 return speed;
429 return sprintf(buf, "%d\n", speed);
432 static ssize_t fan_min_store(struct device *dev,
433 struct device_attribute *attr, const char *buf,
434 size_t count)
436 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
437 struct nct7802_data *data = dev_get_drvdata(dev);
438 unsigned long val;
439 int err;
441 err = kstrtoul(buf, 10, &val);
442 if (err < 0)
443 return err;
445 err = nct7802_write_fan_min(data, sattr->nr, sattr->index, val);
446 return err ? : count;
449 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
450 char *buf)
452 struct nct7802_data *data = dev_get_drvdata(dev);
453 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
454 int bit = sattr->index;
455 unsigned int val;
456 int ret;
458 ret = regmap_read(data->regmap, sattr->nr, &val);
459 if (ret < 0)
460 return ret;
462 return sprintf(buf, "%u\n", !!(val & (1 << bit)));
465 static ssize_t
466 beep_show(struct device *dev, struct device_attribute *attr, char *buf)
468 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
469 struct nct7802_data *data = dev_get_drvdata(dev);
470 unsigned int regval;
471 int err;
473 err = regmap_read(data->regmap, sattr->nr, &regval);
474 if (err)
475 return err;
477 return sprintf(buf, "%u\n", !!(regval & (1 << sattr->index)));
480 static ssize_t
481 beep_store(struct device *dev, struct device_attribute *attr, const char *buf,
482 size_t count)
484 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
485 struct nct7802_data *data = dev_get_drvdata(dev);
486 unsigned long val;
487 int err;
489 err = kstrtoul(buf, 10, &val);
490 if (err < 0)
491 return err;
492 if (val > 1)
493 return -EINVAL;
495 err = regmap_update_bits(data->regmap, sattr->nr, 1 << sattr->index,
496 val ? 1 << sattr->index : 0);
497 return err ? : count;
500 static SENSOR_DEVICE_ATTR_RW(temp1_type, temp_type, 0);
501 static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0x01, REG_TEMP_LSB);
502 static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, 0x31, 0);
503 static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 0x30, 0);
504 static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, 0x3a, 0);
506 static SENSOR_DEVICE_ATTR_RW(temp2_type, temp_type, 1);
507 static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 0x02, REG_TEMP_LSB);
508 static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, 0x33, 0);
509 static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 0x32, 0);
510 static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, 0x3b, 0);
512 static SENSOR_DEVICE_ATTR_RW(temp3_type, temp_type, 2);
513 static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 0x03, REG_TEMP_LSB);
514 static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, 0x35, 0);
515 static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 0x34, 0);
516 static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, 0x3c, 0);
518 static SENSOR_DEVICE_ATTR_2_RO(temp4_input, temp, 0x04, 0);
519 static SENSOR_DEVICE_ATTR_2_RW(temp4_min, temp, 0x37, 0);
520 static SENSOR_DEVICE_ATTR_2_RW(temp4_max, temp, 0x36, 0);
521 static SENSOR_DEVICE_ATTR_2_RW(temp4_crit, temp, 0x3d, 0);
523 static SENSOR_DEVICE_ATTR_2_RO(temp5_input, temp, 0x06, REG_TEMP_PECI_LSB);
524 static SENSOR_DEVICE_ATTR_2_RW(temp5_min, temp, 0x39, 0);
525 static SENSOR_DEVICE_ATTR_2_RW(temp5_max, temp, 0x38, 0);
526 static SENSOR_DEVICE_ATTR_2_RW(temp5_crit, temp, 0x3e, 0);
528 static SENSOR_DEVICE_ATTR_2_RO(temp6_input, temp, 0x07, REG_TEMP_PECI_LSB);
530 static SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, alarm, 0x18, 0);
531 static SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, alarm, 0x18, 1);
532 static SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, alarm, 0x18, 2);
533 static SENSOR_DEVICE_ATTR_2_RO(temp4_min_alarm, alarm, 0x18, 3);
534 static SENSOR_DEVICE_ATTR_2_RO(temp5_min_alarm, alarm, 0x18, 4);
536 static SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, alarm, 0x19, 0);
537 static SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, alarm, 0x19, 1);
538 static SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, alarm, 0x19, 2);
539 static SENSOR_DEVICE_ATTR_2_RO(temp4_max_alarm, alarm, 0x19, 3);
540 static SENSOR_DEVICE_ATTR_2_RO(temp5_max_alarm, alarm, 0x19, 4);
542 static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, alarm, 0x1b, 0);
543 static SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, alarm, 0x1b, 1);
544 static SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, alarm, 0x1b, 2);
545 static SENSOR_DEVICE_ATTR_2_RO(temp4_crit_alarm, alarm, 0x1b, 3);
546 static SENSOR_DEVICE_ATTR_2_RO(temp5_crit_alarm, alarm, 0x1b, 4);
548 static SENSOR_DEVICE_ATTR_2_RO(temp1_fault, alarm, 0x17, 0);
549 static SENSOR_DEVICE_ATTR_2_RO(temp2_fault, alarm, 0x17, 1);
550 static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, alarm, 0x17, 2);
552 static SENSOR_DEVICE_ATTR_2_RW(temp1_beep, beep, 0x5c, 0);
553 static SENSOR_DEVICE_ATTR_2_RW(temp2_beep, beep, 0x5c, 1);
554 static SENSOR_DEVICE_ATTR_2_RW(temp3_beep, beep, 0x5c, 2);
555 static SENSOR_DEVICE_ATTR_2_RW(temp4_beep, beep, 0x5c, 3);
556 static SENSOR_DEVICE_ATTR_2_RW(temp5_beep, beep, 0x5c, 4);
557 static SENSOR_DEVICE_ATTR_2_RW(temp6_beep, beep, 0x5c, 5);
559 static struct attribute *nct7802_temp_attrs[] = {
560 &sensor_dev_attr_temp1_type.dev_attr.attr,
561 &sensor_dev_attr_temp1_input.dev_attr.attr,
562 &sensor_dev_attr_temp1_min.dev_attr.attr,
563 &sensor_dev_attr_temp1_max.dev_attr.attr,
564 &sensor_dev_attr_temp1_crit.dev_attr.attr,
565 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
566 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
567 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
568 &sensor_dev_attr_temp1_fault.dev_attr.attr,
569 &sensor_dev_attr_temp1_beep.dev_attr.attr,
571 &sensor_dev_attr_temp2_type.dev_attr.attr, /* 10 */
572 &sensor_dev_attr_temp2_input.dev_attr.attr,
573 &sensor_dev_attr_temp2_min.dev_attr.attr,
574 &sensor_dev_attr_temp2_max.dev_attr.attr,
575 &sensor_dev_attr_temp2_crit.dev_attr.attr,
576 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
577 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
578 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
579 &sensor_dev_attr_temp2_fault.dev_attr.attr,
580 &sensor_dev_attr_temp2_beep.dev_attr.attr,
582 &sensor_dev_attr_temp3_type.dev_attr.attr, /* 20 */
583 &sensor_dev_attr_temp3_input.dev_attr.attr,
584 &sensor_dev_attr_temp3_min.dev_attr.attr,
585 &sensor_dev_attr_temp3_max.dev_attr.attr,
586 &sensor_dev_attr_temp3_crit.dev_attr.attr,
587 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
588 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
589 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
590 &sensor_dev_attr_temp3_fault.dev_attr.attr,
591 &sensor_dev_attr_temp3_beep.dev_attr.attr,
593 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 30 */
594 &sensor_dev_attr_temp4_min.dev_attr.attr,
595 &sensor_dev_attr_temp4_max.dev_attr.attr,
596 &sensor_dev_attr_temp4_crit.dev_attr.attr,
597 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
598 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
599 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
600 &sensor_dev_attr_temp4_beep.dev_attr.attr,
602 &sensor_dev_attr_temp5_input.dev_attr.attr, /* 38 */
603 &sensor_dev_attr_temp5_min.dev_attr.attr,
604 &sensor_dev_attr_temp5_max.dev_attr.attr,
605 &sensor_dev_attr_temp5_crit.dev_attr.attr,
606 &sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
607 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
608 &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr,
609 &sensor_dev_attr_temp5_beep.dev_attr.attr,
611 &sensor_dev_attr_temp6_input.dev_attr.attr, /* 46 */
612 &sensor_dev_attr_temp6_beep.dev_attr.attr,
614 NULL
617 static umode_t nct7802_temp_is_visible(struct kobject *kobj,
618 struct attribute *attr, int index)
620 struct device *dev = container_of(kobj, struct device, kobj);
621 struct nct7802_data *data = dev_get_drvdata(dev);
622 unsigned int reg;
623 int err;
625 err = regmap_read(data->regmap, REG_MODE, &reg);
626 if (err < 0)
627 return 0;
629 if (index < 10 &&
630 (reg & 03) != 0x01 && (reg & 0x03) != 0x02) /* RD1 */
631 return 0;
633 if (index >= 10 && index < 20 &&
634 (reg & 0x0c) != 0x04 && (reg & 0x0c) != 0x08) /* RD2 */
635 return 0;
636 if (index >= 20 && index < 30 && (reg & 0x30) != 0x20) /* RD3 */
637 return 0;
639 if (index >= 30 && index < 38) /* local */
640 return attr->mode;
642 err = regmap_read(data->regmap, REG_PECI_ENABLE, &reg);
643 if (err < 0)
644 return 0;
646 if (index >= 38 && index < 46 && !(reg & 0x01)) /* PECI 0 */
647 return 0;
649 if (index >= 0x46 && (!(reg & 0x02))) /* PECI 1 */
650 return 0;
652 return attr->mode;
655 static const struct attribute_group nct7802_temp_group = {
656 .attrs = nct7802_temp_attrs,
657 .is_visible = nct7802_temp_is_visible,
660 static SENSOR_DEVICE_ATTR_2_RO(in0_input, in, 0, 0);
661 static SENSOR_DEVICE_ATTR_2_RW(in0_min, in, 0, 1);
662 static SENSOR_DEVICE_ATTR_2_RW(in0_max, in, 0, 2);
663 static SENSOR_DEVICE_ATTR_2_RO(in0_alarm, alarm, 0x1e, 3);
664 static SENSOR_DEVICE_ATTR_2_RW(in0_beep, beep, 0x5a, 3);
666 static SENSOR_DEVICE_ATTR_2_RO(in1_input, in, 1, 0);
668 static SENSOR_DEVICE_ATTR_2_RO(in2_input, in, 2, 0);
669 static SENSOR_DEVICE_ATTR_2_RW(in2_min, in, 2, 1);
670 static SENSOR_DEVICE_ATTR_2_RW(in2_max, in, 2, 2);
671 static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, alarm, 0x1e, 0);
672 static SENSOR_DEVICE_ATTR_2_RW(in2_beep, beep, 0x5a, 0);
674 static SENSOR_DEVICE_ATTR_2_RO(in3_input, in, 3, 0);
675 static SENSOR_DEVICE_ATTR_2_RW(in3_min, in, 3, 1);
676 static SENSOR_DEVICE_ATTR_2_RW(in3_max, in, 3, 2);
677 static SENSOR_DEVICE_ATTR_2_RO(in3_alarm, alarm, 0x1e, 1);
678 static SENSOR_DEVICE_ATTR_2_RW(in3_beep, beep, 0x5a, 1);
680 static SENSOR_DEVICE_ATTR_2_RO(in4_input, in, 4, 0);
681 static SENSOR_DEVICE_ATTR_2_RW(in4_min, in, 4, 1);
682 static SENSOR_DEVICE_ATTR_2_RW(in4_max, in, 4, 2);
683 static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, alarm, 0x1e, 2);
684 static SENSOR_DEVICE_ATTR_2_RW(in4_beep, beep, 0x5a, 2);
686 static struct attribute *nct7802_in_attrs[] = {
687 &sensor_dev_attr_in0_input.dev_attr.attr,
688 &sensor_dev_attr_in0_min.dev_attr.attr,
689 &sensor_dev_attr_in0_max.dev_attr.attr,
690 &sensor_dev_attr_in0_alarm.dev_attr.attr,
691 &sensor_dev_attr_in0_beep.dev_attr.attr,
693 &sensor_dev_attr_in1_input.dev_attr.attr, /* 5 */
695 &sensor_dev_attr_in2_input.dev_attr.attr, /* 6 */
696 &sensor_dev_attr_in2_min.dev_attr.attr,
697 &sensor_dev_attr_in2_max.dev_attr.attr,
698 &sensor_dev_attr_in2_alarm.dev_attr.attr,
699 &sensor_dev_attr_in2_beep.dev_attr.attr,
701 &sensor_dev_attr_in3_input.dev_attr.attr, /* 11 */
702 &sensor_dev_attr_in3_min.dev_attr.attr,
703 &sensor_dev_attr_in3_max.dev_attr.attr,
704 &sensor_dev_attr_in3_alarm.dev_attr.attr,
705 &sensor_dev_attr_in3_beep.dev_attr.attr,
707 &sensor_dev_attr_in4_input.dev_attr.attr, /* 16 */
708 &sensor_dev_attr_in4_min.dev_attr.attr,
709 &sensor_dev_attr_in4_max.dev_attr.attr,
710 &sensor_dev_attr_in4_alarm.dev_attr.attr,
711 &sensor_dev_attr_in4_beep.dev_attr.attr,
713 NULL,
716 static umode_t nct7802_in_is_visible(struct kobject *kobj,
717 struct attribute *attr, int index)
719 struct device *dev = container_of(kobj, struct device, kobj);
720 struct nct7802_data *data = dev_get_drvdata(dev);
721 unsigned int reg;
722 int err;
724 if (index < 6) /* VCC, VCORE */
725 return attr->mode;
727 err = regmap_read(data->regmap, REG_MODE, &reg);
728 if (err < 0)
729 return 0;
731 if (index >= 6 && index < 11 && (reg & 0x03) != 0x03) /* VSEN1 */
732 return 0;
733 if (index >= 11 && index < 16 && (reg & 0x0c) != 0x0c) /* VSEN2 */
734 return 0;
735 if (index >= 16 && (reg & 0x30) != 0x30) /* VSEN3 */
736 return 0;
738 return attr->mode;
741 static const struct attribute_group nct7802_in_group = {
742 .attrs = nct7802_in_attrs,
743 .is_visible = nct7802_in_is_visible,
746 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0x10);
747 static SENSOR_DEVICE_ATTR_2_RW(fan1_min, fan_min, 0x49, 0x4c);
748 static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, alarm, 0x1a, 0);
749 static SENSOR_DEVICE_ATTR_2_RW(fan1_beep, beep, 0x5b, 0);
750 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 0x11);
751 static SENSOR_DEVICE_ATTR_2_RW(fan2_min, fan_min, 0x4a, 0x4d);
752 static SENSOR_DEVICE_ATTR_2_RO(fan2_alarm, alarm, 0x1a, 1);
753 static SENSOR_DEVICE_ATTR_2_RW(fan2_beep, beep, 0x5b, 1);
754 static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 0x12);
755 static SENSOR_DEVICE_ATTR_2_RW(fan3_min, fan_min, 0x4b, 0x4e);
756 static SENSOR_DEVICE_ATTR_2_RO(fan3_alarm, alarm, 0x1a, 2);
757 static SENSOR_DEVICE_ATTR_2_RW(fan3_beep, beep, 0x5b, 2);
759 /* 7.2.89 Fan Control Output Type */
760 static SENSOR_DEVICE_ATTR_RO(pwm1_mode, pwm_mode, 0);
761 static SENSOR_DEVICE_ATTR_RO(pwm2_mode, pwm_mode, 1);
762 static SENSOR_DEVICE_ATTR_RO(pwm3_mode, pwm_mode, 2);
764 /* 7.2.91... Fan Control Output Value */
765 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, REG_PWM(0));
766 static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, REG_PWM(1));
767 static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, REG_PWM(2));
769 /* 7.2.95... Temperature to Fan mapping Relationships Register */
770 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_enable, 0);
771 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_enable, 1);
772 static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_enable, 2);
774 static struct attribute *nct7802_fan_attrs[] = {
775 &sensor_dev_attr_fan1_input.dev_attr.attr,
776 &sensor_dev_attr_fan1_min.dev_attr.attr,
777 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
778 &sensor_dev_attr_fan1_beep.dev_attr.attr,
779 &sensor_dev_attr_fan2_input.dev_attr.attr,
780 &sensor_dev_attr_fan2_min.dev_attr.attr,
781 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
782 &sensor_dev_attr_fan2_beep.dev_attr.attr,
783 &sensor_dev_attr_fan3_input.dev_attr.attr,
784 &sensor_dev_attr_fan3_min.dev_attr.attr,
785 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
786 &sensor_dev_attr_fan3_beep.dev_attr.attr,
788 NULL
791 static umode_t nct7802_fan_is_visible(struct kobject *kobj,
792 struct attribute *attr, int index)
794 struct device *dev = container_of(kobj, struct device, kobj);
795 struct nct7802_data *data = dev_get_drvdata(dev);
796 int fan = index / 4; /* 4 attributes per fan */
797 unsigned int reg;
798 int err;
800 err = regmap_read(data->regmap, REG_FAN_ENABLE, &reg);
801 if (err < 0 || !(reg & (1 << fan)))
802 return 0;
804 return attr->mode;
807 static const struct attribute_group nct7802_fan_group = {
808 .attrs = nct7802_fan_attrs,
809 .is_visible = nct7802_fan_is_visible,
812 static struct attribute *nct7802_pwm_attrs[] = {
813 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
814 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
815 &sensor_dev_attr_pwm1.dev_attr.attr,
816 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
817 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
818 &sensor_dev_attr_pwm2.dev_attr.attr,
819 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
820 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
821 &sensor_dev_attr_pwm3.dev_attr.attr,
822 NULL
825 static const struct attribute_group nct7802_pwm_group = {
826 .attrs = nct7802_pwm_attrs,
829 /* 7.2.115... 0x80-0x83, 0x84 Temperature (X-axis) transition */
830 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, temp, 0x80, 0);
831 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, temp, 0x81, 0);
832 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, temp, 0x82, 0);
833 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, temp, 0x83, 0);
834 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, temp, 0x84, 0);
836 /* 7.2.120... 0x85-0x88 PWM (Y-axis) transition */
837 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm, pwm, 0x85);
838 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm, 0x86);
839 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_pwm, pwm, 0x87);
840 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_pwm, pwm, 0x88);
841 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point5_pwm, pwm, 0);
843 /* 7.2.124 Table 2 X-axis Transition Point 1 Register */
844 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, temp, 0x90, 0);
845 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, temp, 0x91, 0);
846 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, temp, 0x92, 0);
847 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, temp, 0x93, 0);
848 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, temp, 0x94, 0);
850 /* 7.2.129 Table 2 Y-axis Transition Point 1 Register */
851 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm, pwm, 0x95);
852 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm, pwm, 0x96);
853 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point3_pwm, pwm, 0x97);
854 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point4_pwm, pwm, 0x98);
855 static SENSOR_DEVICE_ATTR_RO(pwm2_auto_point5_pwm, pwm, 0);
857 /* 7.2.133 Table 3 X-axis Transition Point 1 Register */
858 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_temp, temp, 0xA0, 0);
859 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_temp, temp, 0xA1, 0);
860 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_temp, temp, 0xA2, 0);
861 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_temp, temp, 0xA3, 0);
862 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_temp, temp, 0xA4, 0);
864 /* 7.2.138 Table 3 Y-axis Transition Point 1 Register */
865 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm, pwm, 0xA5);
866 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm, pwm, 0xA6);
867 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point3_pwm, pwm, 0xA7);
868 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point4_pwm, pwm, 0xA8);
869 static SENSOR_DEVICE_ATTR_RO(pwm3_auto_point5_pwm, pwm, 0);
871 static struct attribute *nct7802_auto_point_attrs[] = {
872 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
873 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
874 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
875 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
876 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
878 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
879 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
880 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
881 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
882 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
884 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
885 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
886 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
887 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
888 &sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
890 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
891 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
892 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
893 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
894 &sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
896 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
897 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
898 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
899 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
900 &sensor_dev_attr_pwm3_auto_point5_temp.dev_attr.attr,
902 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
903 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
904 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
905 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
906 &sensor_dev_attr_pwm3_auto_point5_pwm.dev_attr.attr,
908 NULL
911 static const struct attribute_group nct7802_auto_point_group = {
912 .attrs = nct7802_auto_point_attrs,
915 static const struct attribute_group *nct7802_groups[] = {
916 &nct7802_temp_group,
917 &nct7802_in_group,
918 &nct7802_fan_group,
919 &nct7802_pwm_group,
920 &nct7802_auto_point_group,
921 NULL
924 static int nct7802_detect(struct i2c_client *client,
925 struct i2c_board_info *info)
927 int reg;
930 * Chip identification registers are only available in bank 0,
931 * so only attempt chip detection if bank 0 is selected
933 reg = i2c_smbus_read_byte_data(client, REG_BANK);
934 if (reg != 0x00)
935 return -ENODEV;
937 reg = i2c_smbus_read_byte_data(client, REG_VENDOR_ID);
938 if (reg != 0x50)
939 return -ENODEV;
941 reg = i2c_smbus_read_byte_data(client, REG_CHIP_ID);
942 if (reg != 0xc3)
943 return -ENODEV;
945 reg = i2c_smbus_read_byte_data(client, REG_VERSION_ID);
946 if (reg < 0 || (reg & 0xf0) != 0x20)
947 return -ENODEV;
949 /* Also validate lower bits of voltage and temperature registers */
950 reg = i2c_smbus_read_byte_data(client, REG_TEMP_LSB);
951 if (reg < 0 || (reg & 0x1f))
952 return -ENODEV;
954 reg = i2c_smbus_read_byte_data(client, REG_TEMP_PECI_LSB);
955 if (reg < 0 || (reg & 0x3f))
956 return -ENODEV;
958 reg = i2c_smbus_read_byte_data(client, REG_VOLTAGE_LOW);
959 if (reg < 0 || (reg & 0x3f))
960 return -ENODEV;
962 strlcpy(info->type, "nct7802", I2C_NAME_SIZE);
963 return 0;
966 static bool nct7802_regmap_is_volatile(struct device *dev, unsigned int reg)
968 return (reg != REG_BANK && reg <= 0x20) ||
969 (reg >= REG_PWM(0) && reg <= REG_PWM(2));
972 static const struct regmap_config nct7802_regmap_config = {
973 .reg_bits = 8,
974 .val_bits = 8,
975 .cache_type = REGCACHE_RBTREE,
976 .volatile_reg = nct7802_regmap_is_volatile,
979 static int nct7802_init_chip(struct nct7802_data *data)
981 int err;
983 /* Enable ADC */
984 err = regmap_update_bits(data->regmap, REG_START, 0x01, 0x01);
985 if (err)
986 return err;
988 /* Enable local temperature sensor */
989 err = regmap_update_bits(data->regmap, REG_MODE, 0x40, 0x40);
990 if (err)
991 return err;
993 /* Enable Vcore and VCC voltage monitoring */
994 return regmap_update_bits(data->regmap, REG_VMON_ENABLE, 0x03, 0x03);
997 static int nct7802_probe(struct i2c_client *client,
998 const struct i2c_device_id *id)
1000 struct device *dev = &client->dev;
1001 struct nct7802_data *data;
1002 struct device *hwmon_dev;
1003 int ret;
1005 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
1006 if (data == NULL)
1007 return -ENOMEM;
1009 data->regmap = devm_regmap_init_i2c(client, &nct7802_regmap_config);
1010 if (IS_ERR(data->regmap))
1011 return PTR_ERR(data->regmap);
1013 mutex_init(&data->access_lock);
1015 ret = nct7802_init_chip(data);
1016 if (ret < 0)
1017 return ret;
1019 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1020 data,
1021 nct7802_groups);
1022 return PTR_ERR_OR_ZERO(hwmon_dev);
1025 static const unsigned short nct7802_address_list[] = {
1026 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
1029 static const struct i2c_device_id nct7802_idtable[] = {
1030 { "nct7802", 0 },
1033 MODULE_DEVICE_TABLE(i2c, nct7802_idtable);
1035 static struct i2c_driver nct7802_driver = {
1036 .class = I2C_CLASS_HWMON,
1037 .driver = {
1038 .name = DRVNAME,
1040 .detect = nct7802_detect,
1041 .probe = nct7802_probe,
1042 .id_table = nct7802_idtable,
1043 .address_list = nct7802_address_list,
1046 module_i2c_driver(nct7802_driver);
1048 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
1049 MODULE_DESCRIPTION("NCT7802Y Hardware Monitoring Driver");
1050 MODULE_LICENSE("GPL v2");