xref: /linux/drivers/hwmon/nct7802.c (revision 4b132aacb0768ac1e652cf517097ea6f237214b9)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * nct7802 - Driver for Nuvoton NCT7802Y
4  *
5  * Copyright (C) 2014  Guenter Roeck <linux@roeck-us.net>
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
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>
20 
21 #define DRVNAME "nct7802"
22 
23 static const u8 REG_VOLTAGE[5] = { 0x09, 0x0a, 0x0c, 0x0d, 0x0e };
24 
25 static const u8 REG_VOLTAGE_LIMIT_LSB[2][5] = {
26 	{ 0x46, 0x00, 0x40, 0x42, 0x44 },
27 	{ 0x45, 0x00, 0x3f, 0x41, 0x43 },
28 };
29 
30 static const u8 REG_VOLTAGE_LIMIT_MSB[5] = { 0x48, 0x00, 0x47, 0x47, 0x48 };
31 
32 static const u8 REG_VOLTAGE_LIMIT_MSB_SHIFT[2][5] = {
33 	{ 0, 0, 4, 0, 4 },
34 	{ 2, 0, 6, 2, 6 },
35 };
36 
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
53 
54 /*
55  * Resistance temperature detector (RTD) modes according to 7.2.32 Mode
56  * Selection Register
57  */
58 #define RTD_MODE_CURRENT	0x1
59 #define RTD_MODE_THERMISTOR	0x2
60 #define RTD_MODE_VOLTAGE	0x3
61 
62 #define MODE_RTD_MASK		0x3
63 #define MODE_LTD_EN		0x40
64 
65 /*
66  * Bit offset for sensors modes in REG_MODE.
67  * Valid for index 0..2, indicating RTD1..3.
68  */
69 #define MODE_BIT_OFFSET_RTD(index) ((index) * 2)
70 
71 /*
72  * Data structures and manipulation thereof
73  */
74 
75 struct nct7802_data {
76 	struct regmap *regmap;
77 	struct mutex access_lock; /* for multi-byte read and write operations */
78 	u8 in_status;
79 	struct mutex in_alarm_lock;
80 };
81 
82 static ssize_t temp_type_show(struct device *dev,
83 			      struct device_attribute *attr, char *buf)
84 {
85 	struct nct7802_data *data = dev_get_drvdata(dev);
86 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
87 	unsigned int mode;
88 	int ret;
89 
90 	ret = regmap_read(data->regmap, REG_MODE, &mode);
91 	if (ret < 0)
92 		return ret;
93 
94 	return sprintf(buf, "%u\n", (mode >> (2 * sattr->index) & 3) + 2);
95 }
96 
97 static ssize_t temp_type_store(struct device *dev,
98 			       struct device_attribute *attr, const char *buf,
99 			       size_t count)
100 {
101 	struct nct7802_data *data = dev_get_drvdata(dev);
102 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
103 	unsigned int type;
104 	int err;
105 
106 	err = kstrtouint(buf, 0, &type);
107 	if (err < 0)
108 		return err;
109 	if (sattr->index == 2 && type != 4) /* RD3 */
110 		return -EINVAL;
111 	if (type < 3 || type > 4)
112 		return -EINVAL;
113 	err = regmap_update_bits(data->regmap, REG_MODE,
114 			3 << 2 * sattr->index, (type - 2) << 2 * sattr->index);
115 	return err ? : count;
116 }
117 
118 static ssize_t pwm_mode_show(struct device *dev,
119 			     struct device_attribute *attr, char *buf)
120 {
121 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
122 	struct nct7802_data *data = dev_get_drvdata(dev);
123 	unsigned int regval;
124 	int ret;
125 
126 	if (sattr->index > 1)
127 		return sprintf(buf, "1\n");
128 
129 	ret = regmap_read(data->regmap, 0x5E, &regval);
130 	if (ret < 0)
131 		return ret;
132 
133 	return sprintf(buf, "%u\n", !(regval & (1 << sattr->index)));
134 }
135 
136 static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
137 			char *buf)
138 {
139 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
140 	struct nct7802_data *data = dev_get_drvdata(dev);
141 	unsigned int val;
142 	int ret;
143 
144 	if (!attr->index)
145 		return sprintf(buf, "255\n");
146 
147 	ret = regmap_read(data->regmap, attr->index, &val);
148 	if (ret < 0)
149 		return ret;
150 
151 	return sprintf(buf, "%d\n", val);
152 }
153 
154 static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
155 			 const char *buf, size_t count)
156 {
157 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
158 	struct nct7802_data *data = dev_get_drvdata(dev);
159 	int err;
160 	u8 val;
161 
162 	err = kstrtou8(buf, 0, &val);
163 	if (err < 0)
164 		return err;
165 
166 	err = regmap_write(data->regmap, attr->index, val);
167 	return err ? : count;
168 }
169 
170 static ssize_t pwm_enable_show(struct device *dev,
171 			       struct device_attribute *attr, char *buf)
172 {
173 	struct nct7802_data *data = dev_get_drvdata(dev);
174 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
175 	unsigned int reg, enabled;
176 	int ret;
177 
178 	ret = regmap_read(data->regmap, REG_SMARTFAN_EN(sattr->index), &reg);
179 	if (ret < 0)
180 		return ret;
181 	enabled = reg >> SMARTFAN_EN_SHIFT(sattr->index) & 1;
182 	return sprintf(buf, "%u\n", enabled + 1);
183 }
184 
185 static ssize_t pwm_enable_store(struct device *dev,
186 				struct device_attribute *attr,
187 				const char *buf, size_t count)
188 {
189 	struct nct7802_data *data = dev_get_drvdata(dev);
190 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
191 	u8 val;
192 	int ret;
193 
194 	ret = kstrtou8(buf, 0, &val);
195 	if (ret < 0)
196 		return ret;
197 	if (val < 1 || val > 2)
198 		return -EINVAL;
199 	ret = regmap_update_bits(data->regmap, REG_SMARTFAN_EN(sattr->index),
200 				 1 << SMARTFAN_EN_SHIFT(sattr->index),
201 				 (val - 1) << SMARTFAN_EN_SHIFT(sattr->index));
202 	return ret ? : count;
203 }
204 
205 static int nct7802_read_temp(struct nct7802_data *data,
206 			     u8 reg_temp, u8 reg_temp_low, int *temp)
207 {
208 	unsigned int t1, t2 = 0;
209 	int err;
210 
211 	*temp = 0;
212 
213 	mutex_lock(&data->access_lock);
214 	err = regmap_read(data->regmap, reg_temp, &t1);
215 	if (err < 0)
216 		goto abort;
217 	t1 <<= 8;
218 	if (reg_temp_low) {	/* 11 bit data */
219 		err = regmap_read(data->regmap, reg_temp_low, &t2);
220 		if (err < 0)
221 			goto abort;
222 	}
223 	t1 |= t2 & 0xe0;
224 	*temp = (s16)t1 / 32 * 125;
225 abort:
226 	mutex_unlock(&data->access_lock);
227 	return err;
228 }
229 
230 static int nct7802_read_fan(struct nct7802_data *data, u8 reg_fan)
231 {
232 	unsigned int f1, f2;
233 	int ret;
234 
235 	mutex_lock(&data->access_lock);
236 	ret = regmap_read(data->regmap, reg_fan, &f1);
237 	if (ret < 0)
238 		goto abort;
239 	ret = regmap_read(data->regmap, REG_FANCOUNT_LOW, &f2);
240 	if (ret < 0)
241 		goto abort;
242 	ret = (f1 << 5) | (f2 >> 3);
243 	/* convert fan count to rpm */
244 	if (ret == 0x1fff)	/* maximum value, assume fan is stopped */
245 		ret = 0;
246 	else if (ret)
247 		ret = DIV_ROUND_CLOSEST(1350000U, ret);
248 abort:
249 	mutex_unlock(&data->access_lock);
250 	return ret;
251 }
252 
253 static int nct7802_read_fan_min(struct nct7802_data *data, u8 reg_fan_low,
254 				u8 reg_fan_high)
255 {
256 	unsigned int f1, f2;
257 	int ret;
258 
259 	mutex_lock(&data->access_lock);
260 	ret = regmap_read(data->regmap, reg_fan_low, &f1);
261 	if (ret < 0)
262 		goto abort;
263 	ret = regmap_read(data->regmap, reg_fan_high, &f2);
264 	if (ret < 0)
265 		goto abort;
266 	ret = f1 | ((f2 & 0xf8) << 5);
267 	/* convert fan count to rpm */
268 	if (ret == 0x1fff)	/* maximum value, assume no limit */
269 		ret = 0;
270 	else if (ret)
271 		ret = DIV_ROUND_CLOSEST(1350000U, ret);
272 	else
273 		ret = 1350000U;
274 abort:
275 	mutex_unlock(&data->access_lock);
276 	return ret;
277 }
278 
279 static int nct7802_write_fan_min(struct nct7802_data *data, u8 reg_fan_low,
280 				 u8 reg_fan_high, unsigned long limit)
281 {
282 	int err;
283 
284 	if (limit)
285 		limit = DIV_ROUND_CLOSEST(1350000U, limit);
286 	else
287 		limit = 0x1fff;
288 	limit = clamp_val(limit, 0, 0x1fff);
289 
290 	mutex_lock(&data->access_lock);
291 	err = regmap_write(data->regmap, reg_fan_low, limit & 0xff);
292 	if (err < 0)
293 		goto abort;
294 
295 	err = regmap_write(data->regmap, reg_fan_high, (limit & 0x1f00) >> 5);
296 abort:
297 	mutex_unlock(&data->access_lock);
298 	return err;
299 }
300 
301 static u8 nct7802_vmul[] = { 4, 2, 2, 2, 2 };
302 
303 static int nct7802_read_voltage(struct nct7802_data *data, int nr, int index)
304 {
305 	unsigned int v1, v2;
306 	int ret;
307 
308 	mutex_lock(&data->access_lock);
309 	if (index == 0) {	/* voltage */
310 		ret = regmap_read(data->regmap, REG_VOLTAGE[nr], &v1);
311 		if (ret < 0)
312 			goto abort;
313 		ret = regmap_read(data->regmap, REG_VOLTAGE_LOW, &v2);
314 		if (ret < 0)
315 			goto abort;
316 		ret = ((v1 << 2) | (v2 >> 6)) * nct7802_vmul[nr];
317 	}  else {	/* limit */
318 		int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
319 
320 		ret = regmap_read(data->regmap,
321 				  REG_VOLTAGE_LIMIT_LSB[index - 1][nr], &v1);
322 		if (ret < 0)
323 			goto abort;
324 		ret = regmap_read(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
325 				  &v2);
326 		if (ret < 0)
327 			goto abort;
328 		ret = (v1 | ((v2 << shift) & 0x300)) * nct7802_vmul[nr];
329 	}
330 abort:
331 	mutex_unlock(&data->access_lock);
332 	return ret;
333 }
334 
335 static int nct7802_write_voltage(struct nct7802_data *data, int nr, int index,
336 				 unsigned long voltage)
337 {
338 	int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
339 	int err;
340 
341 	voltage = clamp_val(voltage, 0, 0x3ff * nct7802_vmul[nr]);
342 	voltage = DIV_ROUND_CLOSEST(voltage, nct7802_vmul[nr]);
343 
344 	mutex_lock(&data->access_lock);
345 	err = regmap_write(data->regmap,
346 			   REG_VOLTAGE_LIMIT_LSB[index - 1][nr],
347 			   voltage & 0xff);
348 	if (err < 0)
349 		goto abort;
350 
351 	err = regmap_update_bits(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
352 				 0x0300 >> shift, (voltage & 0x0300) >> shift);
353 abort:
354 	mutex_unlock(&data->access_lock);
355 	return err;
356 }
357 
358 static ssize_t in_show(struct device *dev, struct device_attribute *attr,
359 		       char *buf)
360 {
361 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
362 	struct nct7802_data *data = dev_get_drvdata(dev);
363 	int voltage;
364 
365 	voltage = nct7802_read_voltage(data, sattr->nr, sattr->index);
366 	if (voltage < 0)
367 		return voltage;
368 
369 	return sprintf(buf, "%d\n", voltage);
370 }
371 
372 static ssize_t in_store(struct device *dev, struct device_attribute *attr,
373 			const char *buf, size_t count)
374 {
375 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
376 	struct nct7802_data *data = dev_get_drvdata(dev);
377 	int index = sattr->index;
378 	int nr = sattr->nr;
379 	unsigned long val;
380 	int err;
381 
382 	err = kstrtoul(buf, 10, &val);
383 	if (err < 0)
384 		return err;
385 
386 	err = nct7802_write_voltage(data, nr, index, val);
387 	return err ? : count;
388 }
389 
390 static ssize_t in_alarm_show(struct device *dev, struct device_attribute *attr,
391 			     char *buf)
392 {
393 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
394 	struct nct7802_data *data = dev_get_drvdata(dev);
395 	int volt, min, max, ret;
396 	unsigned int val;
397 
398 	mutex_lock(&data->in_alarm_lock);
399 
400 	/*
401 	 * The SMI Voltage status register is the only register giving a status
402 	 * for voltages. A bit is set for each input crossing a threshold, in
403 	 * both direction, but the "inside" or "outside" limits info is not
404 	 * available. Also this register is cleared on read.
405 	 * Note: this is not explicitly spelled out in the datasheet, but
406 	 * from experiment.
407 	 * To deal with this we use a status cache with one validity bit and
408 	 * one status bit for each input. Validity is cleared at startup and
409 	 * each time the register reports a change, and the status is processed
410 	 * by software based on current input value and limits.
411 	 */
412 	ret = regmap_read(data->regmap, 0x1e, &val); /* SMI Voltage status */
413 	if (ret < 0)
414 		goto abort;
415 
416 	/* invalidate cached status for all inputs crossing a threshold */
417 	data->in_status &= ~((val & 0x0f) << 4);
418 
419 	/* if cached status for requested input is invalid, update it */
420 	if (!(data->in_status & (0x10 << sattr->index))) {
421 		ret = nct7802_read_voltage(data, sattr->nr, 0);
422 		if (ret < 0)
423 			goto abort;
424 		volt = ret;
425 
426 		ret = nct7802_read_voltage(data, sattr->nr, 1);
427 		if (ret < 0)
428 			goto abort;
429 		min = ret;
430 
431 		ret = nct7802_read_voltage(data, sattr->nr, 2);
432 		if (ret < 0)
433 			goto abort;
434 		max = ret;
435 
436 		if (volt < min || volt > max)
437 			data->in_status |= (1 << sattr->index);
438 		else
439 			data->in_status &= ~(1 << sattr->index);
440 
441 		data->in_status |= 0x10 << sattr->index;
442 	}
443 
444 	ret = sprintf(buf, "%u\n", !!(data->in_status & (1 << sattr->index)));
445 abort:
446 	mutex_unlock(&data->in_alarm_lock);
447 	return ret;
448 }
449 
450 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
451 			 char *buf)
452 {
453 	struct nct7802_data *data = dev_get_drvdata(dev);
454 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
455 	int err, temp;
456 
457 	err = nct7802_read_temp(data, sattr->nr, sattr->index, &temp);
458 	if (err < 0)
459 		return err;
460 
461 	return sprintf(buf, "%d\n", temp);
462 }
463 
464 static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
465 			  const char *buf, size_t count)
466 {
467 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
468 	struct nct7802_data *data = dev_get_drvdata(dev);
469 	int nr = sattr->nr;
470 	long val;
471 	int err;
472 
473 	err = kstrtol(buf, 10, &val);
474 	if (err < 0)
475 		return err;
476 
477 	val = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
478 
479 	err = regmap_write(data->regmap, nr, val & 0xff);
480 	return err ? : count;
481 }
482 
483 static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
484 			char *buf)
485 {
486 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
487 	struct nct7802_data *data = dev_get_drvdata(dev);
488 	int speed;
489 
490 	speed = nct7802_read_fan(data, sattr->index);
491 	if (speed < 0)
492 		return speed;
493 
494 	return sprintf(buf, "%d\n", speed);
495 }
496 
497 static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
498 			    char *buf)
499 {
500 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
501 	struct nct7802_data *data = dev_get_drvdata(dev);
502 	int speed;
503 
504 	speed = nct7802_read_fan_min(data, sattr->nr, sattr->index);
505 	if (speed < 0)
506 		return speed;
507 
508 	return sprintf(buf, "%d\n", speed);
509 }
510 
511 static ssize_t fan_min_store(struct device *dev,
512 			     struct device_attribute *attr, const char *buf,
513 			     size_t count)
514 {
515 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
516 	struct nct7802_data *data = dev_get_drvdata(dev);
517 	unsigned long val;
518 	int err;
519 
520 	err = kstrtoul(buf, 10, &val);
521 	if (err < 0)
522 		return err;
523 
524 	err = nct7802_write_fan_min(data, sattr->nr, sattr->index, val);
525 	return err ? : count;
526 }
527 
528 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
529 			  char *buf)
530 {
531 	struct nct7802_data *data = dev_get_drvdata(dev);
532 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
533 	int bit = sattr->index;
534 	unsigned int val;
535 	int ret;
536 
537 	ret = regmap_read(data->regmap, sattr->nr, &val);
538 	if (ret < 0)
539 		return ret;
540 
541 	return sprintf(buf, "%u\n", !!(val & (1 << bit)));
542 }
543 
544 static ssize_t
545 beep_show(struct device *dev, struct device_attribute *attr, char *buf)
546 {
547 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
548 	struct nct7802_data *data = dev_get_drvdata(dev);
549 	unsigned int regval;
550 	int err;
551 
552 	err = regmap_read(data->regmap, sattr->nr, &regval);
553 	if (err)
554 		return err;
555 
556 	return sprintf(buf, "%u\n", !!(regval & (1 << sattr->index)));
557 }
558 
559 static ssize_t
560 beep_store(struct device *dev, struct device_attribute *attr, const char *buf,
561 	   size_t count)
562 {
563 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
564 	struct nct7802_data *data = dev_get_drvdata(dev);
565 	unsigned long val;
566 	int err;
567 
568 	err = kstrtoul(buf, 10, &val);
569 	if (err < 0)
570 		return err;
571 	if (val > 1)
572 		return -EINVAL;
573 
574 	err = regmap_update_bits(data->regmap, sattr->nr, 1 << sattr->index,
575 				 val ? 1 << sattr->index : 0);
576 	return err ? : count;
577 }
578 
579 static SENSOR_DEVICE_ATTR_RW(temp1_type, temp_type, 0);
580 static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0x01, REG_TEMP_LSB);
581 static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, 0x31, 0);
582 static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 0x30, 0);
583 static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, 0x3a, 0);
584 
585 static SENSOR_DEVICE_ATTR_RW(temp2_type, temp_type, 1);
586 static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 0x02, REG_TEMP_LSB);
587 static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, 0x33, 0);
588 static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 0x32, 0);
589 static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, 0x3b, 0);
590 
591 static SENSOR_DEVICE_ATTR_RW(temp3_type, temp_type, 2);
592 static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 0x03, REG_TEMP_LSB);
593 static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, 0x35, 0);
594 static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 0x34, 0);
595 static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, 0x3c, 0);
596 
597 static SENSOR_DEVICE_ATTR_2_RO(temp4_input, temp, 0x04, 0);
598 static SENSOR_DEVICE_ATTR_2_RW(temp4_min, temp, 0x37, 0);
599 static SENSOR_DEVICE_ATTR_2_RW(temp4_max, temp, 0x36, 0);
600 static SENSOR_DEVICE_ATTR_2_RW(temp4_crit, temp, 0x3d, 0);
601 
602 static SENSOR_DEVICE_ATTR_2_RO(temp5_input, temp, 0x06, REG_TEMP_PECI_LSB);
603 static SENSOR_DEVICE_ATTR_2_RW(temp5_min, temp, 0x39, 0);
604 static SENSOR_DEVICE_ATTR_2_RW(temp5_max, temp, 0x38, 0);
605 static SENSOR_DEVICE_ATTR_2_RW(temp5_crit, temp, 0x3e, 0);
606 
607 static SENSOR_DEVICE_ATTR_2_RO(temp6_input, temp, 0x07, REG_TEMP_PECI_LSB);
608 
609 static SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, alarm, 0x18, 0);
610 static SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, alarm, 0x18, 1);
611 static SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, alarm, 0x18, 2);
612 static SENSOR_DEVICE_ATTR_2_RO(temp4_min_alarm, alarm, 0x18, 3);
613 static SENSOR_DEVICE_ATTR_2_RO(temp5_min_alarm, alarm, 0x18, 4);
614 
615 static SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, alarm, 0x19, 0);
616 static SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, alarm, 0x19, 1);
617 static SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, alarm, 0x19, 2);
618 static SENSOR_DEVICE_ATTR_2_RO(temp4_max_alarm, alarm, 0x19, 3);
619 static SENSOR_DEVICE_ATTR_2_RO(temp5_max_alarm, alarm, 0x19, 4);
620 
621 static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, alarm, 0x1b, 0);
622 static SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, alarm, 0x1b, 1);
623 static SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, alarm, 0x1b, 2);
624 static SENSOR_DEVICE_ATTR_2_RO(temp4_crit_alarm, alarm, 0x1b, 3);
625 static SENSOR_DEVICE_ATTR_2_RO(temp5_crit_alarm, alarm, 0x1b, 4);
626 
627 static SENSOR_DEVICE_ATTR_2_RO(temp1_fault, alarm, 0x17, 0);
628 static SENSOR_DEVICE_ATTR_2_RO(temp2_fault, alarm, 0x17, 1);
629 static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, alarm, 0x17, 2);
630 
631 static SENSOR_DEVICE_ATTR_2_RW(temp1_beep, beep, 0x5c, 0);
632 static SENSOR_DEVICE_ATTR_2_RW(temp2_beep, beep, 0x5c, 1);
633 static SENSOR_DEVICE_ATTR_2_RW(temp3_beep, beep, 0x5c, 2);
634 static SENSOR_DEVICE_ATTR_2_RW(temp4_beep, beep, 0x5c, 3);
635 static SENSOR_DEVICE_ATTR_2_RW(temp5_beep, beep, 0x5c, 4);
636 static SENSOR_DEVICE_ATTR_2_RW(temp6_beep, beep, 0x5c, 5);
637 
638 static struct attribute *nct7802_temp_attrs[] = {
639 	&sensor_dev_attr_temp1_type.dev_attr.attr,
640 	&sensor_dev_attr_temp1_input.dev_attr.attr,
641 	&sensor_dev_attr_temp1_min.dev_attr.attr,
642 	&sensor_dev_attr_temp1_max.dev_attr.attr,
643 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
644 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
645 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
646 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
647 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
648 	&sensor_dev_attr_temp1_beep.dev_attr.attr,
649 
650 	&sensor_dev_attr_temp2_type.dev_attr.attr,		/* 10 */
651 	&sensor_dev_attr_temp2_input.dev_attr.attr,
652 	&sensor_dev_attr_temp2_min.dev_attr.attr,
653 	&sensor_dev_attr_temp2_max.dev_attr.attr,
654 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
655 	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
656 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
657 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
658 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
659 	&sensor_dev_attr_temp2_beep.dev_attr.attr,
660 
661 	&sensor_dev_attr_temp3_type.dev_attr.attr,		/* 20 */
662 	&sensor_dev_attr_temp3_input.dev_attr.attr,
663 	&sensor_dev_attr_temp3_min.dev_attr.attr,
664 	&sensor_dev_attr_temp3_max.dev_attr.attr,
665 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
666 	&sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
667 	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
668 	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
669 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
670 	&sensor_dev_attr_temp3_beep.dev_attr.attr,
671 
672 	&sensor_dev_attr_temp4_input.dev_attr.attr,		/* 30 */
673 	&sensor_dev_attr_temp4_min.dev_attr.attr,
674 	&sensor_dev_attr_temp4_max.dev_attr.attr,
675 	&sensor_dev_attr_temp4_crit.dev_attr.attr,
676 	&sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
677 	&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
678 	&sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
679 	&sensor_dev_attr_temp4_beep.dev_attr.attr,
680 
681 	&sensor_dev_attr_temp5_input.dev_attr.attr,		/* 38 */
682 	&sensor_dev_attr_temp5_min.dev_attr.attr,
683 	&sensor_dev_attr_temp5_max.dev_attr.attr,
684 	&sensor_dev_attr_temp5_crit.dev_attr.attr,
685 	&sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
686 	&sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
687 	&sensor_dev_attr_temp5_crit_alarm.dev_attr.attr,
688 	&sensor_dev_attr_temp5_beep.dev_attr.attr,
689 
690 	&sensor_dev_attr_temp6_input.dev_attr.attr,		/* 46 */
691 	&sensor_dev_attr_temp6_beep.dev_attr.attr,
692 
693 	NULL
694 };
695 
696 static umode_t nct7802_temp_is_visible(struct kobject *kobj,
697 				       struct attribute *attr, int index)
698 {
699 	struct device *dev = kobj_to_dev(kobj);
700 	struct nct7802_data *data = dev_get_drvdata(dev);
701 	unsigned int reg;
702 	int err;
703 
704 	err = regmap_read(data->regmap, REG_MODE, &reg);
705 	if (err < 0)
706 		return 0;
707 
708 	if (index < 10 &&
709 	    (reg & 03) != 0x01 && (reg & 0x03) != 0x02)		/* RD1 */
710 		return 0;
711 
712 	if (index >= 10 && index < 20 &&
713 	    (reg & 0x0c) != 0x04 && (reg & 0x0c) != 0x08)	/* RD2 */
714 		return 0;
715 	if (index >= 20 && index < 30 && (reg & 0x30) != 0x20)	/* RD3 */
716 		return 0;
717 
718 	if (index >= 30 && index < 38)				/* local */
719 		return attr->mode;
720 
721 	err = regmap_read(data->regmap, REG_PECI_ENABLE, &reg);
722 	if (err < 0)
723 		return 0;
724 
725 	if (index >= 38 && index < 46 && !(reg & 0x01))		/* PECI 0 */
726 		return 0;
727 
728 	if (index >= 46 && !(reg & 0x02))			/* PECI 1 */
729 		return 0;
730 
731 	return attr->mode;
732 }
733 
734 static const struct attribute_group nct7802_temp_group = {
735 	.attrs = nct7802_temp_attrs,
736 	.is_visible = nct7802_temp_is_visible,
737 };
738 
739 static SENSOR_DEVICE_ATTR_2_RO(in0_input, in, 0, 0);
740 static SENSOR_DEVICE_ATTR_2_RW(in0_min, in, 0, 1);
741 static SENSOR_DEVICE_ATTR_2_RW(in0_max, in, 0, 2);
742 static SENSOR_DEVICE_ATTR_2_RO(in0_alarm, in_alarm, 0, 3);
743 static SENSOR_DEVICE_ATTR_2_RW(in0_beep, beep, 0x5a, 3);
744 
745 static SENSOR_DEVICE_ATTR_2_RO(in1_input, in, 1, 0);
746 
747 static SENSOR_DEVICE_ATTR_2_RO(in2_input, in, 2, 0);
748 static SENSOR_DEVICE_ATTR_2_RW(in2_min, in, 2, 1);
749 static SENSOR_DEVICE_ATTR_2_RW(in2_max, in, 2, 2);
750 static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, in_alarm, 2, 0);
751 static SENSOR_DEVICE_ATTR_2_RW(in2_beep, beep, 0x5a, 0);
752 
753 static SENSOR_DEVICE_ATTR_2_RO(in3_input, in, 3, 0);
754 static SENSOR_DEVICE_ATTR_2_RW(in3_min, in, 3, 1);
755 static SENSOR_DEVICE_ATTR_2_RW(in3_max, in, 3, 2);
756 static SENSOR_DEVICE_ATTR_2_RO(in3_alarm, in_alarm, 3, 1);
757 static SENSOR_DEVICE_ATTR_2_RW(in3_beep, beep, 0x5a, 1);
758 
759 static SENSOR_DEVICE_ATTR_2_RO(in4_input, in, 4, 0);
760 static SENSOR_DEVICE_ATTR_2_RW(in4_min, in, 4, 1);
761 static SENSOR_DEVICE_ATTR_2_RW(in4_max, in, 4, 2);
762 static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, in_alarm, 4, 2);
763 static SENSOR_DEVICE_ATTR_2_RW(in4_beep, beep, 0x5a, 2);
764 
765 static struct attribute *nct7802_in_attrs[] = {
766 	&sensor_dev_attr_in0_input.dev_attr.attr,
767 	&sensor_dev_attr_in0_min.dev_attr.attr,
768 	&sensor_dev_attr_in0_max.dev_attr.attr,
769 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
770 	&sensor_dev_attr_in0_beep.dev_attr.attr,
771 
772 	&sensor_dev_attr_in1_input.dev_attr.attr,	/* 5 */
773 
774 	&sensor_dev_attr_in2_input.dev_attr.attr,	/* 6 */
775 	&sensor_dev_attr_in2_min.dev_attr.attr,
776 	&sensor_dev_attr_in2_max.dev_attr.attr,
777 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
778 	&sensor_dev_attr_in2_beep.dev_attr.attr,
779 
780 	&sensor_dev_attr_in3_input.dev_attr.attr,	/* 11 */
781 	&sensor_dev_attr_in3_min.dev_attr.attr,
782 	&sensor_dev_attr_in3_max.dev_attr.attr,
783 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
784 	&sensor_dev_attr_in3_beep.dev_attr.attr,
785 
786 	&sensor_dev_attr_in4_input.dev_attr.attr,	/* 16 */
787 	&sensor_dev_attr_in4_min.dev_attr.attr,
788 	&sensor_dev_attr_in4_max.dev_attr.attr,
789 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
790 	&sensor_dev_attr_in4_beep.dev_attr.attr,
791 
792 	NULL,
793 };
794 
795 static umode_t nct7802_in_is_visible(struct kobject *kobj,
796 				     struct attribute *attr, int index)
797 {
798 	struct device *dev = kobj_to_dev(kobj);
799 	struct nct7802_data *data = dev_get_drvdata(dev);
800 	unsigned int reg;
801 	int err;
802 
803 	if (index < 6)						/* VCC, VCORE */
804 		return attr->mode;
805 
806 	err = regmap_read(data->regmap, REG_MODE, &reg);
807 	if (err < 0)
808 		return 0;
809 
810 	if (index >= 6 && index < 11 && (reg & 0x03) != 0x03)	/* VSEN1 */
811 		return 0;
812 	if (index >= 11 && index < 16 && (reg & 0x0c) != 0x0c)	/* VSEN2 */
813 		return 0;
814 	if (index >= 16 && (reg & 0x30) != 0x30)		/* VSEN3 */
815 		return 0;
816 
817 	return attr->mode;
818 }
819 
820 static const struct attribute_group nct7802_in_group = {
821 	.attrs = nct7802_in_attrs,
822 	.is_visible = nct7802_in_is_visible,
823 };
824 
825 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0x10);
826 static SENSOR_DEVICE_ATTR_2_RW(fan1_min, fan_min, 0x49, 0x4c);
827 static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, alarm, 0x1a, 0);
828 static SENSOR_DEVICE_ATTR_2_RW(fan1_beep, beep, 0x5b, 0);
829 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 0x11);
830 static SENSOR_DEVICE_ATTR_2_RW(fan2_min, fan_min, 0x4a, 0x4d);
831 static SENSOR_DEVICE_ATTR_2_RO(fan2_alarm, alarm, 0x1a, 1);
832 static SENSOR_DEVICE_ATTR_2_RW(fan2_beep, beep, 0x5b, 1);
833 static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 0x12);
834 static SENSOR_DEVICE_ATTR_2_RW(fan3_min, fan_min, 0x4b, 0x4e);
835 static SENSOR_DEVICE_ATTR_2_RO(fan3_alarm, alarm, 0x1a, 2);
836 static SENSOR_DEVICE_ATTR_2_RW(fan3_beep, beep, 0x5b, 2);
837 
838 /* 7.2.89 Fan Control Output Type */
839 static SENSOR_DEVICE_ATTR_RO(pwm1_mode, pwm_mode, 0);
840 static SENSOR_DEVICE_ATTR_RO(pwm2_mode, pwm_mode, 1);
841 static SENSOR_DEVICE_ATTR_RO(pwm3_mode, pwm_mode, 2);
842 
843 /* 7.2.91... Fan Control Output Value */
844 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, REG_PWM(0));
845 static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, REG_PWM(1));
846 static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, REG_PWM(2));
847 
848 /* 7.2.95... Temperature to Fan mapping Relationships Register */
849 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_enable, 0);
850 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_enable, 1);
851 static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_enable, 2);
852 
853 static struct attribute *nct7802_fan_attrs[] = {
854 	&sensor_dev_attr_fan1_input.dev_attr.attr,
855 	&sensor_dev_attr_fan1_min.dev_attr.attr,
856 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
857 	&sensor_dev_attr_fan1_beep.dev_attr.attr,
858 	&sensor_dev_attr_fan2_input.dev_attr.attr,
859 	&sensor_dev_attr_fan2_min.dev_attr.attr,
860 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
861 	&sensor_dev_attr_fan2_beep.dev_attr.attr,
862 	&sensor_dev_attr_fan3_input.dev_attr.attr,
863 	&sensor_dev_attr_fan3_min.dev_attr.attr,
864 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
865 	&sensor_dev_attr_fan3_beep.dev_attr.attr,
866 
867 	NULL
868 };
869 
870 static umode_t nct7802_fan_is_visible(struct kobject *kobj,
871 				      struct attribute *attr, int index)
872 {
873 	struct device *dev = kobj_to_dev(kobj);
874 	struct nct7802_data *data = dev_get_drvdata(dev);
875 	int fan = index / 4;	/* 4 attributes per fan */
876 	unsigned int reg;
877 	int err;
878 
879 	err = regmap_read(data->regmap, REG_FAN_ENABLE, &reg);
880 	if (err < 0 || !(reg & (1 << fan)))
881 		return 0;
882 
883 	return attr->mode;
884 }
885 
886 static const struct attribute_group nct7802_fan_group = {
887 	.attrs = nct7802_fan_attrs,
888 	.is_visible = nct7802_fan_is_visible,
889 };
890 
891 static struct attribute *nct7802_pwm_attrs[] = {
892 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
893 	&sensor_dev_attr_pwm1_mode.dev_attr.attr,
894 	&sensor_dev_attr_pwm1.dev_attr.attr,
895 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
896 	&sensor_dev_attr_pwm2_mode.dev_attr.attr,
897 	&sensor_dev_attr_pwm2.dev_attr.attr,
898 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
899 	&sensor_dev_attr_pwm3_mode.dev_attr.attr,
900 	&sensor_dev_attr_pwm3.dev_attr.attr,
901 	NULL
902 };
903 
904 static const struct attribute_group nct7802_pwm_group = {
905 	.attrs = nct7802_pwm_attrs,
906 };
907 
908 /* 7.2.115... 0x80-0x83, 0x84 Temperature (X-axis) transition */
909 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, temp, 0x80, 0);
910 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, temp, 0x81, 0);
911 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, temp, 0x82, 0);
912 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, temp, 0x83, 0);
913 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, temp, 0x84, 0);
914 
915 /* 7.2.120... 0x85-0x88 PWM (Y-axis) transition */
916 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm, pwm, 0x85);
917 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm, 0x86);
918 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_pwm, pwm, 0x87);
919 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_pwm, pwm, 0x88);
920 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point5_pwm, pwm, 0);
921 
922 /* 7.2.124 Table 2 X-axis Transition Point 1 Register */
923 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, temp, 0x90, 0);
924 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, temp, 0x91, 0);
925 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, temp, 0x92, 0);
926 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, temp, 0x93, 0);
927 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, temp, 0x94, 0);
928 
929 /* 7.2.129 Table 2 Y-axis Transition Point 1 Register */
930 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm, pwm, 0x95);
931 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm, pwm, 0x96);
932 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point3_pwm, pwm, 0x97);
933 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point4_pwm, pwm, 0x98);
934 static SENSOR_DEVICE_ATTR_RO(pwm2_auto_point5_pwm, pwm, 0);
935 
936 /* 7.2.133 Table 3 X-axis Transition Point 1 Register */
937 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_temp, temp, 0xA0, 0);
938 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_temp, temp, 0xA1, 0);
939 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_temp, temp, 0xA2, 0);
940 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_temp, temp, 0xA3, 0);
941 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_temp, temp, 0xA4, 0);
942 
943 /* 7.2.138 Table 3 Y-axis Transition Point 1 Register */
944 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm, pwm, 0xA5);
945 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm, pwm, 0xA6);
946 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point3_pwm, pwm, 0xA7);
947 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point4_pwm, pwm, 0xA8);
948 static SENSOR_DEVICE_ATTR_RO(pwm3_auto_point5_pwm, pwm, 0);
949 
950 static struct attribute *nct7802_auto_point_attrs[] = {
951 	&sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
952 	&sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
953 	&sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
954 	&sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
955 	&sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
956 
957 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
958 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
959 	&sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
960 	&sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
961 	&sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
962 
963 	&sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
964 	&sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
965 	&sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
966 	&sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
967 	&sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
968 
969 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
970 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
971 	&sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
972 	&sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
973 	&sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
974 
975 	&sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
976 	&sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
977 	&sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
978 	&sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
979 	&sensor_dev_attr_pwm3_auto_point5_temp.dev_attr.attr,
980 
981 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
982 	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
983 	&sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
984 	&sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
985 	&sensor_dev_attr_pwm3_auto_point5_pwm.dev_attr.attr,
986 
987 	NULL
988 };
989 
990 static const struct attribute_group nct7802_auto_point_group = {
991 	.attrs = nct7802_auto_point_attrs,
992 };
993 
994 static const struct attribute_group *nct7802_groups[] = {
995 	&nct7802_temp_group,
996 	&nct7802_in_group,
997 	&nct7802_fan_group,
998 	&nct7802_pwm_group,
999 	&nct7802_auto_point_group,
1000 	NULL
1001 };
1002 
1003 static int nct7802_detect(struct i2c_client *client,
1004 			  struct i2c_board_info *info)
1005 {
1006 	int reg;
1007 
1008 	/*
1009 	 * Chip identification registers are only available in bank 0,
1010 	 * so only attempt chip detection if bank 0 is selected
1011 	 */
1012 	reg = i2c_smbus_read_byte_data(client, REG_BANK);
1013 	if (reg != 0x00)
1014 		return -ENODEV;
1015 
1016 	reg = i2c_smbus_read_byte_data(client, REG_VENDOR_ID);
1017 	if (reg != 0x50)
1018 		return -ENODEV;
1019 
1020 	reg = i2c_smbus_read_byte_data(client, REG_CHIP_ID);
1021 	if (reg != 0xc3)
1022 		return -ENODEV;
1023 
1024 	reg = i2c_smbus_read_byte_data(client, REG_VERSION_ID);
1025 	if (reg < 0 || (reg & 0xf0) != 0x20)
1026 		return -ENODEV;
1027 
1028 	/* Also validate lower bits of voltage and temperature registers */
1029 	reg = i2c_smbus_read_byte_data(client, REG_TEMP_LSB);
1030 	if (reg < 0 || (reg & 0x1f))
1031 		return -ENODEV;
1032 
1033 	reg = i2c_smbus_read_byte_data(client, REG_TEMP_PECI_LSB);
1034 	if (reg < 0 || (reg & 0x3f))
1035 		return -ENODEV;
1036 
1037 	reg = i2c_smbus_read_byte_data(client, REG_VOLTAGE_LOW);
1038 	if (reg < 0 || (reg & 0x3f))
1039 		return -ENODEV;
1040 
1041 	strscpy(info->type, "nct7802", I2C_NAME_SIZE);
1042 	return 0;
1043 }
1044 
1045 static bool nct7802_regmap_is_volatile(struct device *dev, unsigned int reg)
1046 {
1047 	return (reg != REG_BANK && reg <= 0x20) ||
1048 		(reg >= REG_PWM(0) && reg <= REG_PWM(2));
1049 }
1050 
1051 static const struct regmap_config nct7802_regmap_config = {
1052 	.reg_bits = 8,
1053 	.val_bits = 8,
1054 	.cache_type = REGCACHE_MAPLE,
1055 	.volatile_reg = nct7802_regmap_is_volatile,
1056 };
1057 
1058 static int nct7802_get_channel_config(struct device *dev,
1059 				      struct device_node *node, u8 *mode_mask,
1060 				      u8 *mode_val)
1061 {
1062 	u32 reg;
1063 	const char *type_str, *md_str;
1064 	u8 md;
1065 
1066 	if (!node->name || of_node_cmp(node->name, "channel"))
1067 		return 0;
1068 
1069 	if (of_property_read_u32(node, "reg", &reg)) {
1070 		dev_err(dev, "Could not read reg value for '%s'\n",
1071 			node->full_name);
1072 		return -EINVAL;
1073 	}
1074 
1075 	if (reg > 3) {
1076 		dev_err(dev, "Invalid reg (%u) in '%s'\n", reg,
1077 			node->full_name);
1078 		return -EINVAL;
1079 	}
1080 
1081 	if (reg == 0) {
1082 		if (!of_device_is_available(node))
1083 			*mode_val &= ~MODE_LTD_EN;
1084 		else
1085 			*mode_val |= MODE_LTD_EN;
1086 		*mode_mask |= MODE_LTD_EN;
1087 		return 0;
1088 	}
1089 
1090 	/* At this point we have reg >= 1 && reg <= 3 */
1091 
1092 	if (!of_device_is_available(node)) {
1093 		*mode_val &= ~(MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1));
1094 		*mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1);
1095 		return 0;
1096 	}
1097 
1098 	if (of_property_read_string(node, "sensor-type", &type_str)) {
1099 		dev_err(dev, "No type for '%s'\n", node->full_name);
1100 		return -EINVAL;
1101 	}
1102 
1103 	if (!strcmp(type_str, "voltage")) {
1104 		*mode_val |= (RTD_MODE_VOLTAGE & MODE_RTD_MASK)
1105 			     << MODE_BIT_OFFSET_RTD(reg - 1);
1106 		*mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1);
1107 		return 0;
1108 	}
1109 
1110 	if (strcmp(type_str, "temperature")) {
1111 		dev_err(dev, "Invalid type '%s' for '%s'\n", type_str,
1112 			node->full_name);
1113 		return -EINVAL;
1114 	}
1115 
1116 	if (reg == 3) {
1117 		/* RTD3 only supports thermistor mode */
1118 		md = RTD_MODE_THERMISTOR;
1119 	} else {
1120 		if (of_property_read_string(node, "temperature-mode",
1121 					    &md_str)) {
1122 			dev_err(dev, "No mode for '%s'\n", node->full_name);
1123 			return -EINVAL;
1124 		}
1125 
1126 		if (!strcmp(md_str, "thermal-diode"))
1127 			md = RTD_MODE_CURRENT;
1128 		else if (!strcmp(md_str, "thermistor"))
1129 			md = RTD_MODE_THERMISTOR;
1130 		else {
1131 			dev_err(dev, "Invalid mode '%s' for '%s'\n", md_str,
1132 				node->full_name);
1133 			return -EINVAL;
1134 		}
1135 	}
1136 
1137 	*mode_val |= (md & MODE_RTD_MASK) << MODE_BIT_OFFSET_RTD(reg - 1);
1138 	*mode_mask |= MODE_RTD_MASK << MODE_BIT_OFFSET_RTD(reg - 1);
1139 
1140 	return 0;
1141 }
1142 
1143 static int nct7802_configure_channels(struct device *dev,
1144 				      struct nct7802_data *data)
1145 {
1146 	/* Enable local temperature sensor by default */
1147 	u8 mode_mask = MODE_LTD_EN, mode_val = MODE_LTD_EN;
1148 	struct device_node *node;
1149 	int err;
1150 
1151 	if (dev->of_node) {
1152 		for_each_child_of_node(dev->of_node, node) {
1153 			err = nct7802_get_channel_config(dev, node, &mode_mask,
1154 							 &mode_val);
1155 			if (err) {
1156 				of_node_put(node);
1157 				return err;
1158 			}
1159 		}
1160 	}
1161 
1162 	return regmap_update_bits(data->regmap, REG_MODE, mode_mask, mode_val);
1163 }
1164 
1165 static int nct7802_init_chip(struct device *dev, struct nct7802_data *data)
1166 {
1167 	int err;
1168 
1169 	/* Enable ADC */
1170 	err = regmap_update_bits(data->regmap, REG_START, 0x01, 0x01);
1171 	if (err)
1172 		return err;
1173 
1174 	err = nct7802_configure_channels(dev, data);
1175 	if (err)
1176 		return err;
1177 
1178 	/* Enable Vcore and VCC voltage monitoring */
1179 	return regmap_update_bits(data->regmap, REG_VMON_ENABLE, 0x03, 0x03);
1180 }
1181 
1182 static int nct7802_probe(struct i2c_client *client)
1183 {
1184 	struct device *dev = &client->dev;
1185 	struct nct7802_data *data;
1186 	struct device *hwmon_dev;
1187 	int ret;
1188 
1189 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
1190 	if (data == NULL)
1191 		return -ENOMEM;
1192 
1193 	data->regmap = devm_regmap_init_i2c(client, &nct7802_regmap_config);
1194 	if (IS_ERR(data->regmap))
1195 		return PTR_ERR(data->regmap);
1196 
1197 	mutex_init(&data->access_lock);
1198 	mutex_init(&data->in_alarm_lock);
1199 
1200 	ret = nct7802_init_chip(dev, data);
1201 	if (ret < 0)
1202 		return ret;
1203 
1204 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1205 							   data,
1206 							   nct7802_groups);
1207 	return PTR_ERR_OR_ZERO(hwmon_dev);
1208 }
1209 
1210 static const unsigned short nct7802_address_list[] = {
1211 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
1212 };
1213 
1214 static const struct i2c_device_id nct7802_idtable[] = {
1215 	{ "nct7802" },
1216 	{ }
1217 };
1218 MODULE_DEVICE_TABLE(i2c, nct7802_idtable);
1219 
1220 static struct i2c_driver nct7802_driver = {
1221 	.class = I2C_CLASS_HWMON,
1222 	.driver = {
1223 		.name = DRVNAME,
1224 	},
1225 	.detect = nct7802_detect,
1226 	.probe = nct7802_probe,
1227 	.id_table = nct7802_idtable,
1228 	.address_list = nct7802_address_list,
1229 };
1230 
1231 module_i2c_driver(nct7802_driver);
1232 
1233 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
1234 MODULE_DESCRIPTION("NCT7802Y Hardware Monitoring Driver");
1235 MODULE_LICENSE("GPL v2");
1236