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