xref: /openbmc/linux/drivers/hwmon/nct6683.c (revision dfe94d40)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * nct6683 - Driver for the hardware monitoring functionality of
4  *	     Nuvoton NCT6683D/NCT6687D eSIO
5  *
6  * Copyright (C) 2013  Guenter Roeck <linux@roeck-us.net>
7  *
8  * Derived from nct6775 driver
9  * Copyright (C) 2012, 2013  Guenter Roeck <linux@roeck-us.net>
10  *
11  * Supports the following chips:
12  *
13  * Chip        #vin    #fan    #pwm    #temp  chip ID
14  * nct6683d     21(1)   16      8       32(1) 0xc730
15  * nct6687d     21(1)   16      8       32(1) 0xd590
16  *
17  * Notes:
18  *	(1) Total number of vin and temp inputs is 32.
19  */
20 
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 
23 #include <linux/acpi.h>
24 #include <linux/delay.h>
25 #include <linux/err.h>
26 #include <linux/init.h>
27 #include <linux/io.h>
28 #include <linux/jiffies.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/module.h>
32 #include <linux/mutex.h>
33 #include <linux/platform_device.h>
34 #include <linux/slab.h>
35 
36 enum kinds { nct6683, nct6687 };
37 
38 static bool force;
39 module_param(force, bool, 0);
40 MODULE_PARM_DESC(force, "Set to one to enable support for unknown vendors");
41 
42 static const char * const nct6683_device_names[] = {
43 	"nct6683",
44 	"nct6687",
45 };
46 
47 static const char * const nct6683_chip_names[] = {
48 	"NCT6683D",
49 	"NCT6687D",
50 };
51 
52 #define DRVNAME "nct6683"
53 
54 /*
55  * Super-I/O constants and functions
56  */
57 
58 #define NCT6683_LD_ACPI		0x0a
59 #define NCT6683_LD_HWM		0x0b
60 #define NCT6683_LD_VID		0x0d
61 
62 #define SIO_REG_LDSEL		0x07	/* Logical device select */
63 #define SIO_REG_DEVID		0x20	/* Device ID (2 bytes) */
64 #define SIO_REG_ENABLE		0x30	/* Logical device enable */
65 #define SIO_REG_ADDR		0x60	/* Logical device address (2 bytes) */
66 
67 #define SIO_NCT6681_ID		0xb270	/* for later */
68 #define SIO_NCT6683_ID		0xc730
69 #define SIO_NCT6687_ID		0xd590
70 #define SIO_ID_MASK		0xFFF0
71 
72 static inline void
73 superio_outb(int ioreg, int reg, int val)
74 {
75 	outb(reg, ioreg);
76 	outb(val, ioreg + 1);
77 }
78 
79 static inline int
80 superio_inb(int ioreg, int reg)
81 {
82 	outb(reg, ioreg);
83 	return inb(ioreg + 1);
84 }
85 
86 static inline void
87 superio_select(int ioreg, int ld)
88 {
89 	outb(SIO_REG_LDSEL, ioreg);
90 	outb(ld, ioreg + 1);
91 }
92 
93 static inline int
94 superio_enter(int ioreg)
95 {
96 	/*
97 	 * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
98 	 */
99 	if (!request_muxed_region(ioreg, 2, DRVNAME))
100 		return -EBUSY;
101 
102 	outb(0x87, ioreg);
103 	outb(0x87, ioreg);
104 
105 	return 0;
106 }
107 
108 static inline void
109 superio_exit(int ioreg)
110 {
111 	outb(0xaa, ioreg);
112 	outb(0x02, ioreg);
113 	outb(0x02, ioreg + 1);
114 	release_region(ioreg, 2);
115 }
116 
117 /*
118  * ISA constants
119  */
120 
121 #define IOREGION_ALIGNMENT	(~7)
122 #define IOREGION_OFFSET		4	/* Use EC port 1 */
123 #define IOREGION_LENGTH		4
124 
125 #define EC_PAGE_REG		0
126 #define EC_INDEX_REG		1
127 #define EC_DATA_REG		2
128 #define EC_EVENT_REG		3
129 
130 /* Common and NCT6683 specific data */
131 
132 #define NCT6683_NUM_REG_MON		32
133 #define NCT6683_NUM_REG_FAN		16
134 #define NCT6683_NUM_REG_PWM		8
135 
136 #define NCT6683_REG_MON(x)		(0x100 + (x) * 2)
137 #define NCT6683_REG_FAN_RPM(x)		(0x140 + (x) * 2)
138 #define NCT6683_REG_PWM(x)		(0x160 + (x))
139 #define NCT6683_REG_PWM_WRITE(x)	(0xa28 + (x))
140 
141 #define NCT6683_REG_MON_STS(x)		(0x174 + (x))
142 #define NCT6683_REG_IDLE(x)		(0x178 + (x))
143 
144 #define NCT6683_REG_FAN_STS(x)		(0x17c + (x))
145 #define NCT6683_REG_FAN_ERRSTS		0x17e
146 #define NCT6683_REG_FAN_INITSTS		0x17f
147 
148 #define NCT6683_HWM_CFG			0x180
149 
150 #define NCT6683_REG_MON_CFG(x)		(0x1a0 + (x))
151 #define NCT6683_REG_FANIN_CFG(x)	(0x1c0 + (x))
152 #define NCT6683_REG_FANOUT_CFG(x)	(0x1d0 + (x))
153 
154 #define NCT6683_REG_INTEL_TEMP_MAX(x)	(0x901 + (x) * 16)
155 #define NCT6683_REG_INTEL_TEMP_CRIT(x)	(0x90d + (x) * 16)
156 
157 #define NCT6683_REG_TEMP_HYST(x)	(0x330 + (x))		/* 8 bit */
158 #define NCT6683_REG_TEMP_MAX(x)		(0x350 + (x))		/* 8 bit */
159 #define NCT6683_REG_MON_HIGH(x)		(0x370 + (x) * 2)	/* 8 bit */
160 #define NCT6683_REG_MON_LOW(x)		(0x371 + (x) * 2)	/* 8 bit */
161 
162 #define NCT6683_REG_FAN_MIN(x)		(0x3b8 + (x) * 2)	/* 16 bit */
163 
164 #define NCT6683_REG_FAN_CFG_CTRL	0xa01
165 #define NCT6683_FAN_CFG_REQ		0x80
166 #define NCT6683_FAN_CFG_DONE		0x40
167 
168 #define NCT6683_REG_CUSTOMER_ID		0x602
169 #define NCT6683_CUSTOMER_ID_INTEL	0x805
170 #define NCT6683_CUSTOMER_ID_MITAC	0xa0e
171 #define NCT6683_CUSTOMER_ID_MSI		0x201
172 
173 #define NCT6683_REG_BUILD_YEAR		0x604
174 #define NCT6683_REG_BUILD_MONTH		0x605
175 #define NCT6683_REG_BUILD_DAY		0x606
176 #define NCT6683_REG_SERIAL		0x607
177 #define NCT6683_REG_VERSION_HI		0x608
178 #define NCT6683_REG_VERSION_LO		0x609
179 
180 #define NCT6683_REG_CR_CASEOPEN		0xe8
181 #define NCT6683_CR_CASEOPEN_MASK	(1 << 7)
182 
183 #define NCT6683_REG_CR_BEEP		0xe0
184 #define NCT6683_CR_BEEP_MASK		(1 << 6)
185 
186 static const char *const nct6683_mon_label[] = {
187 	NULL,	/* disabled */
188 	"Local",
189 	"Diode 0 (curr)",
190 	"Diode 1 (curr)",
191 	"Diode 2 (curr)",
192 	"Diode 0 (volt)",
193 	"Diode 1 (volt)",
194 	"Diode 2 (volt)",
195 	"Thermistor 14",
196 	"Thermistor 15",
197 	"Thermistor 16",
198 	"Thermistor 0",
199 	"Thermistor 1",
200 	"Thermistor 2",
201 	"Thermistor 3",
202 	"Thermistor 4",
203 	"Thermistor 5",		/* 0x10 */
204 	"Thermistor 6",
205 	"Thermistor 7",
206 	"Thermistor 8",
207 	"Thermistor 9",
208 	"Thermistor 10",
209 	"Thermistor 11",
210 	"Thermistor 12",
211 	"Thermistor 13",
212 	NULL, NULL, NULL, NULL, NULL, NULL, NULL,
213 	"PECI 0.0",		/* 0x20 */
214 	"PECI 1.0",
215 	"PECI 2.0",
216 	"PECI 3.0",
217 	"PECI 0.1",
218 	"PECI 1.1",
219 	"PECI 2.1",
220 	"PECI 3.1",
221 	"PECI DIMM 0",
222 	"PECI DIMM 1",
223 	"PECI DIMM 2",
224 	"PECI DIMM 3",
225 	NULL, NULL, NULL, NULL,
226 	"PCH CPU",		/* 0x30 */
227 	"PCH CHIP",
228 	"PCH CHIP CPU MAX",
229 	"PCH MCH",
230 	"PCH DIMM 0",
231 	"PCH DIMM 1",
232 	"PCH DIMM 2",
233 	"PCH DIMM 3",
234 	"SMBus 0",
235 	"SMBus 1",
236 	"SMBus 2",
237 	"SMBus 3",
238 	"SMBus 4",
239 	"SMBus 5",
240 	"DIMM 0",
241 	"DIMM 1",
242 	"DIMM 2",		/* 0x40 */
243 	"DIMM 3",
244 	"AMD TSI Addr 90h",
245 	"AMD TSI Addr 92h",
246 	"AMD TSI Addr 94h",
247 	"AMD TSI Addr 96h",
248 	"AMD TSI Addr 98h",
249 	"AMD TSI Addr 9ah",
250 	"AMD TSI Addr 9ch",
251 	"AMD TSI Addr 9dh",
252 	NULL, NULL, NULL, NULL, NULL, NULL,
253 	"Virtual 0",		/* 0x50 */
254 	"Virtual 1",
255 	"Virtual 2",
256 	"Virtual 3",
257 	"Virtual 4",
258 	"Virtual 5",
259 	"Virtual 6",
260 	"Virtual 7",
261 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
262 	"VCC",			/* 0x60 voltage sensors */
263 	"VSB",
264 	"AVSB",
265 	"VTT",
266 	"VBAT",
267 	"VREF",
268 	"VIN0",
269 	"VIN1",
270 	"VIN2",
271 	"VIN3",
272 	"VIN4",
273 	"VIN5",
274 	"VIN6",
275 	"VIN7",
276 	"VIN8",
277 	"VIN9",
278 	"VIN10",
279 	"VIN11",
280 	"VIN12",
281 	"VIN13",
282 	"VIN14",
283 	"VIN15",
284 	"VIN16",
285 };
286 
287 #define NUM_MON_LABELS		ARRAY_SIZE(nct6683_mon_label)
288 #define MON_VOLTAGE_START	0x60
289 
290 /* ------------------------------------------------------- */
291 
292 struct nct6683_data {
293 	int addr;		/* IO base of EC space */
294 	int sioreg;		/* SIO register */
295 	enum kinds kind;
296 	u16 customer_id;
297 
298 	struct device *hwmon_dev;
299 	const struct attribute_group *groups[6];
300 
301 	int temp_num;			/* number of temperature attributes */
302 	u8 temp_index[NCT6683_NUM_REG_MON];
303 	u8 temp_src[NCT6683_NUM_REG_MON];
304 
305 	u8 in_num;			/* number of voltage attributes */
306 	u8 in_index[NCT6683_NUM_REG_MON];
307 	u8 in_src[NCT6683_NUM_REG_MON];
308 
309 	struct mutex update_lock;	/* used to protect sensor updates */
310 	bool valid;			/* true if following fields are valid */
311 	unsigned long last_updated;	/* In jiffies */
312 
313 	/* Voltage attribute values */
314 	u8 in[3][NCT6683_NUM_REG_MON];	/* [0]=in, [1]=in_max, [2]=in_min */
315 
316 	/* Temperature attribute values */
317 	s16 temp_in[NCT6683_NUM_REG_MON];
318 	s8 temp[4][NCT6683_NUM_REG_MON];/* [0]=min, [1]=max, [2]=hyst,
319 					 * [3]=crit
320 					 */
321 
322 	/* Fan attribute values */
323 	unsigned int rpm[NCT6683_NUM_REG_FAN];
324 	u16 fan_min[NCT6683_NUM_REG_FAN];
325 	u8 fanin_cfg[NCT6683_NUM_REG_FAN];
326 	u8 fanout_cfg[NCT6683_NUM_REG_FAN];
327 	u16 have_fan;			/* some fan inputs can be disabled */
328 
329 	u8 have_pwm;
330 	u8 pwm[NCT6683_NUM_REG_PWM];
331 
332 #ifdef CONFIG_PM
333 	/* Remember extra register values over suspend/resume */
334 	u8 hwm_cfg;
335 #endif
336 };
337 
338 struct nct6683_sio_data {
339 	int sioreg;
340 	enum kinds kind;
341 };
342 
343 struct sensor_device_template {
344 	struct device_attribute dev_attr;
345 	union {
346 		struct {
347 			u8 nr;
348 			u8 index;
349 		} s;
350 		int index;
351 	} u;
352 	bool s2;	/* true if both index and nr are used */
353 };
354 
355 struct sensor_device_attr_u {
356 	union {
357 		struct sensor_device_attribute a1;
358 		struct sensor_device_attribute_2 a2;
359 	} u;
360 	char name[32];
361 };
362 
363 #define __TEMPLATE_ATTR(_template, _mode, _show, _store) {	\
364 	.attr = {.name = _template, .mode = _mode },		\
365 	.show	= _show,					\
366 	.store	= _store,					\
367 }
368 
369 #define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index)	\
370 	{ .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store),	\
371 	  .u.index = _index,						\
372 	  .s2 = false }
373 
374 #define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store,	\
375 				 _nr, _index)				\
376 	{ .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store),	\
377 	  .u.s.index = _index,						\
378 	  .u.s.nr = _nr,						\
379 	  .s2 = true }
380 
381 #define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index)	\
382 static struct sensor_device_template sensor_dev_template_##_name	\
383 	= SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store,	\
384 				 _index)
385 
386 #define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store,	\
387 			  _nr, _index)					\
388 static struct sensor_device_template sensor_dev_template_##_name	\
389 	= SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store,	\
390 				 _nr, _index)
391 
392 struct sensor_template_group {
393 	struct sensor_device_template **templates;
394 	umode_t (*is_visible)(struct kobject *, struct attribute *, int);
395 	int base;
396 };
397 
398 static struct attribute_group *
399 nct6683_create_attr_group(struct device *dev,
400 			  const struct sensor_template_group *tg,
401 			  int repeat)
402 {
403 	struct sensor_device_attribute_2 *a2;
404 	struct sensor_device_attribute *a;
405 	struct sensor_device_template **t;
406 	struct sensor_device_attr_u *su;
407 	struct attribute_group *group;
408 	struct attribute **attrs;
409 	int i, j, count;
410 
411 	if (repeat <= 0)
412 		return ERR_PTR(-EINVAL);
413 
414 	t = tg->templates;
415 	for (count = 0; *t; t++, count++)
416 		;
417 
418 	if (count == 0)
419 		return ERR_PTR(-EINVAL);
420 
421 	group = devm_kzalloc(dev, sizeof(*group), GFP_KERNEL);
422 	if (group == NULL)
423 		return ERR_PTR(-ENOMEM);
424 
425 	attrs = devm_kcalloc(dev, repeat * count + 1, sizeof(*attrs),
426 			     GFP_KERNEL);
427 	if (attrs == NULL)
428 		return ERR_PTR(-ENOMEM);
429 
430 	su = devm_kzalloc(dev, array3_size(repeat, count, sizeof(*su)),
431 			  GFP_KERNEL);
432 	if (su == NULL)
433 		return ERR_PTR(-ENOMEM);
434 
435 	group->attrs = attrs;
436 	group->is_visible = tg->is_visible;
437 
438 	for (i = 0; i < repeat; i++) {
439 		t = tg->templates;
440 		for (j = 0; *t != NULL; j++) {
441 			snprintf(su->name, sizeof(su->name),
442 				 (*t)->dev_attr.attr.name, tg->base + i);
443 			if ((*t)->s2) {
444 				a2 = &su->u.a2;
445 				sysfs_attr_init(&a2->dev_attr.attr);
446 				a2->dev_attr.attr.name = su->name;
447 				a2->nr = (*t)->u.s.nr + i;
448 				a2->index = (*t)->u.s.index;
449 				a2->dev_attr.attr.mode =
450 				  (*t)->dev_attr.attr.mode;
451 				a2->dev_attr.show = (*t)->dev_attr.show;
452 				a2->dev_attr.store = (*t)->dev_attr.store;
453 				*attrs = &a2->dev_attr.attr;
454 			} else {
455 				a = &su->u.a1;
456 				sysfs_attr_init(&a->dev_attr.attr);
457 				a->dev_attr.attr.name = su->name;
458 				a->index = (*t)->u.index + i;
459 				a->dev_attr.attr.mode =
460 				  (*t)->dev_attr.attr.mode;
461 				a->dev_attr.show = (*t)->dev_attr.show;
462 				a->dev_attr.store = (*t)->dev_attr.store;
463 				*attrs = &a->dev_attr.attr;
464 			}
465 			attrs++;
466 			su++;
467 			t++;
468 		}
469 	}
470 
471 	return group;
472 }
473 
474 /* LSB is 16 mV, except for the following sources, where it is 32 mV */
475 #define MON_SRC_VCC	0x60
476 #define MON_SRC_VSB	0x61
477 #define MON_SRC_AVSB	0x62
478 #define MON_SRC_VBAT	0x64
479 
480 static inline long in_from_reg(u16 reg, u8 src)
481 {
482 	int scale = 16;
483 
484 	if (src == MON_SRC_VCC || src == MON_SRC_VSB || src == MON_SRC_AVSB ||
485 	    src == MON_SRC_VBAT)
486 		scale <<= 1;
487 	return reg * scale;
488 }
489 
490 static inline u16 in_to_reg(u32 val, u8 src)
491 {
492 	int scale = 16;
493 
494 	if (src == MON_SRC_VCC || src == MON_SRC_VSB || src == MON_SRC_AVSB ||
495 	    src == MON_SRC_VBAT)
496 		scale <<= 1;
497 
498 	return clamp_val(DIV_ROUND_CLOSEST(val, scale), 0, 127);
499 }
500 
501 static u16 nct6683_read(struct nct6683_data *data, u16 reg)
502 {
503 	int res;
504 
505 	outb_p(0xff, data->addr + EC_PAGE_REG);		/* unlock */
506 	outb_p(reg >> 8, data->addr + EC_PAGE_REG);
507 	outb_p(reg & 0xff, data->addr + EC_INDEX_REG);
508 	res = inb_p(data->addr + EC_DATA_REG);
509 	return res;
510 }
511 
512 static u16 nct6683_read16(struct nct6683_data *data, u16 reg)
513 {
514 	return (nct6683_read(data, reg) << 8) | nct6683_read(data, reg + 1);
515 }
516 
517 static void nct6683_write(struct nct6683_data *data, u16 reg, u16 value)
518 {
519 	outb_p(0xff, data->addr + EC_PAGE_REG);		/* unlock */
520 	outb_p(reg >> 8, data->addr + EC_PAGE_REG);
521 	outb_p(reg & 0xff, data->addr + EC_INDEX_REG);
522 	outb_p(value & 0xff, data->addr + EC_DATA_REG);
523 }
524 
525 static int get_in_reg(struct nct6683_data *data, int nr, int index)
526 {
527 	int ch = data->in_index[index];
528 	int reg = -EINVAL;
529 
530 	switch (nr) {
531 	case 0:
532 		reg = NCT6683_REG_MON(ch);
533 		break;
534 	case 1:
535 		if (data->customer_id != NCT6683_CUSTOMER_ID_INTEL)
536 			reg = NCT6683_REG_MON_LOW(ch);
537 		break;
538 	case 2:
539 		if (data->customer_id != NCT6683_CUSTOMER_ID_INTEL)
540 			reg = NCT6683_REG_MON_HIGH(ch);
541 		break;
542 	default:
543 		break;
544 	}
545 	return reg;
546 }
547 
548 static int get_temp_reg(struct nct6683_data *data, int nr, int index)
549 {
550 	int ch = data->temp_index[index];
551 	int reg = -EINVAL;
552 
553 	switch (data->customer_id) {
554 	case NCT6683_CUSTOMER_ID_INTEL:
555 		switch (nr) {
556 		default:
557 		case 1:	/* max */
558 			reg = NCT6683_REG_INTEL_TEMP_MAX(ch);
559 			break;
560 		case 3:	/* crit */
561 			reg = NCT6683_REG_INTEL_TEMP_CRIT(ch);
562 			break;
563 		}
564 		break;
565 	case NCT6683_CUSTOMER_ID_MITAC:
566 	default:
567 		switch (nr) {
568 		default:
569 		case 0:	/* min */
570 			reg = NCT6683_REG_MON_LOW(ch);
571 			break;
572 		case 1:	/* max */
573 			reg = NCT6683_REG_TEMP_MAX(ch);
574 			break;
575 		case 2:	/* hyst */
576 			reg = NCT6683_REG_TEMP_HYST(ch);
577 			break;
578 		case 3:	/* crit */
579 			reg = NCT6683_REG_MON_HIGH(ch);
580 			break;
581 		}
582 		break;
583 	}
584 	return reg;
585 }
586 
587 static void nct6683_update_pwm(struct device *dev)
588 {
589 	struct nct6683_data *data = dev_get_drvdata(dev);
590 	int i;
591 
592 	for (i = 0; i < NCT6683_NUM_REG_PWM; i++) {
593 		if (!(data->have_pwm & (1 << i)))
594 			continue;
595 		data->pwm[i] = nct6683_read(data, NCT6683_REG_PWM(i));
596 	}
597 }
598 
599 static struct nct6683_data *nct6683_update_device(struct device *dev)
600 {
601 	struct nct6683_data *data = dev_get_drvdata(dev);
602 	int i, j;
603 
604 	mutex_lock(&data->update_lock);
605 
606 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
607 		/* Measured voltages and limits */
608 		for (i = 0; i < data->in_num; i++) {
609 			for (j = 0; j < 3; j++) {
610 				int reg = get_in_reg(data, j, i);
611 
612 				if (reg >= 0)
613 					data->in[j][i] =
614 						nct6683_read(data, reg);
615 			}
616 		}
617 
618 		/* Measured temperatures and limits */
619 		for (i = 0; i < data->temp_num; i++) {
620 			u8 ch = data->temp_index[i];
621 
622 			data->temp_in[i] = nct6683_read16(data,
623 							  NCT6683_REG_MON(ch));
624 			for (j = 0; j < 4; j++) {
625 				int reg = get_temp_reg(data, j, i);
626 
627 				if (reg >= 0)
628 					data->temp[j][i] =
629 						nct6683_read(data, reg);
630 			}
631 		}
632 
633 		/* Measured fan speeds and limits */
634 		for (i = 0; i < ARRAY_SIZE(data->rpm); i++) {
635 			if (!(data->have_fan & (1 << i)))
636 				continue;
637 
638 			data->rpm[i] = nct6683_read16(data,
639 						NCT6683_REG_FAN_RPM(i));
640 			data->fan_min[i] = nct6683_read16(data,
641 						NCT6683_REG_FAN_MIN(i));
642 		}
643 
644 		nct6683_update_pwm(dev);
645 
646 		data->last_updated = jiffies;
647 		data->valid = true;
648 	}
649 
650 	mutex_unlock(&data->update_lock);
651 	return data;
652 }
653 
654 /*
655  * Sysfs callback functions
656  */
657 static ssize_t
658 show_in_label(struct device *dev, struct device_attribute *attr, char *buf)
659 {
660 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
661 	struct nct6683_data *data = nct6683_update_device(dev);
662 	int nr = sattr->index;
663 
664 	return sprintf(buf, "%s\n", nct6683_mon_label[data->in_src[nr]]);
665 }
666 
667 static ssize_t
668 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
669 {
670 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
671 	struct nct6683_data *data = nct6683_update_device(dev);
672 	int index = sattr->index;
673 	int nr = sattr->nr;
674 
675 	return sprintf(buf, "%ld\n",
676 		       in_from_reg(data->in[index][nr], data->in_index[index]));
677 }
678 
679 static umode_t nct6683_in_is_visible(struct kobject *kobj,
680 				     struct attribute *attr, int index)
681 {
682 	struct device *dev = kobj_to_dev(kobj);
683 	struct nct6683_data *data = dev_get_drvdata(dev);
684 	int nr = index % 4;	/* attribute */
685 
686 	/*
687 	 * Voltage limits exist for Intel boards,
688 	 * but register location and encoding is unknown
689 	 */
690 	if ((nr == 2 || nr == 3) &&
691 	    data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
692 		return 0;
693 
694 	return attr->mode;
695 }
696 
697 SENSOR_TEMPLATE(in_label, "in%d_label", S_IRUGO, show_in_label, NULL, 0);
698 SENSOR_TEMPLATE_2(in_input, "in%d_input", S_IRUGO, show_in_reg, NULL, 0, 0);
699 SENSOR_TEMPLATE_2(in_min, "in%d_min", S_IRUGO, show_in_reg, NULL, 0, 1);
700 SENSOR_TEMPLATE_2(in_max, "in%d_max", S_IRUGO, show_in_reg, NULL, 0, 2);
701 
702 static struct sensor_device_template *nct6683_attributes_in_template[] = {
703 	&sensor_dev_template_in_label,
704 	&sensor_dev_template_in_input,
705 	&sensor_dev_template_in_min,
706 	&sensor_dev_template_in_max,
707 	NULL
708 };
709 
710 static const struct sensor_template_group nct6683_in_template_group = {
711 	.templates = nct6683_attributes_in_template,
712 	.is_visible = nct6683_in_is_visible,
713 };
714 
715 static ssize_t
716 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
717 {
718 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
719 	struct nct6683_data *data = nct6683_update_device(dev);
720 
721 	return sprintf(buf, "%d\n", data->rpm[sattr->index]);
722 }
723 
724 static ssize_t
725 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
726 {
727 	struct nct6683_data *data = nct6683_update_device(dev);
728 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
729 	int nr = sattr->index;
730 
731 	return sprintf(buf, "%d\n", data->fan_min[nr]);
732 }
733 
734 static ssize_t
735 show_fan_pulses(struct device *dev, struct device_attribute *attr, char *buf)
736 {
737 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
738 	struct nct6683_data *data = nct6683_update_device(dev);
739 
740 	return sprintf(buf, "%d\n",
741 		       ((data->fanin_cfg[sattr->index] >> 5) & 0x03) + 1);
742 }
743 
744 static umode_t nct6683_fan_is_visible(struct kobject *kobj,
745 				      struct attribute *attr, int index)
746 {
747 	struct device *dev = kobj_to_dev(kobj);
748 	struct nct6683_data *data = dev_get_drvdata(dev);
749 	int fan = index / 3;	/* fan index */
750 	int nr = index % 3;	/* attribute index */
751 
752 	if (!(data->have_fan & (1 << fan)))
753 		return 0;
754 
755 	/*
756 	 * Intel may have minimum fan speed limits,
757 	 * but register location and encoding are unknown.
758 	 */
759 	if (nr == 2 && data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
760 		return 0;
761 
762 	return attr->mode;
763 }
764 
765 SENSOR_TEMPLATE(fan_input, "fan%d_input", S_IRUGO, show_fan, NULL, 0);
766 SENSOR_TEMPLATE(fan_pulses, "fan%d_pulses", S_IRUGO, show_fan_pulses, NULL, 0);
767 SENSOR_TEMPLATE(fan_min, "fan%d_min", S_IRUGO, show_fan_min, NULL, 0);
768 
769 /*
770  * nct6683_fan_is_visible uses the index into the following array
771  * to determine if attributes should be created or not.
772  * Any change in order or content must be matched.
773  */
774 static struct sensor_device_template *nct6683_attributes_fan_template[] = {
775 	&sensor_dev_template_fan_input,
776 	&sensor_dev_template_fan_pulses,
777 	&sensor_dev_template_fan_min,
778 	NULL
779 };
780 
781 static const struct sensor_template_group nct6683_fan_template_group = {
782 	.templates = nct6683_attributes_fan_template,
783 	.is_visible = nct6683_fan_is_visible,
784 	.base = 1,
785 };
786 
787 static ssize_t
788 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
789 {
790 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
791 	struct nct6683_data *data = nct6683_update_device(dev);
792 	int nr = sattr->index;
793 
794 	return sprintf(buf, "%s\n", nct6683_mon_label[data->temp_src[nr]]);
795 }
796 
797 static ssize_t
798 show_temp8(struct device *dev, struct device_attribute *attr, char *buf)
799 {
800 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
801 	struct nct6683_data *data = nct6683_update_device(dev);
802 	int index = sattr->index;
803 	int nr = sattr->nr;
804 
805 	return sprintf(buf, "%d\n", data->temp[index][nr] * 1000);
806 }
807 
808 static ssize_t
809 show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
810 {
811 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
812 	struct nct6683_data *data = nct6683_update_device(dev);
813 	int nr = sattr->index;
814 	int temp = data->temp[1][nr] - data->temp[2][nr];
815 
816 	return sprintf(buf, "%d\n", temp * 1000);
817 }
818 
819 static ssize_t
820 show_temp16(struct device *dev, struct device_attribute *attr, char *buf)
821 {
822 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
823 	struct nct6683_data *data = nct6683_update_device(dev);
824 	int index = sattr->index;
825 
826 	return sprintf(buf, "%d\n", (data->temp_in[index] / 128) * 500);
827 }
828 
829 /*
830  * Temperature sensor type is determined by temperature source
831  * and can not be modified.
832  * 0x02..0x07: Thermal diode
833  * 0x08..0x18: Thermistor
834  * 0x20..0x2b: Intel PECI
835  * 0x42..0x49: AMD TSI
836  * Others are unspecified (not visible)
837  */
838 
839 static int get_temp_type(u8 src)
840 {
841 	if (src >= 0x02 && src <= 0x07)
842 		return 3;	/* thermal diode */
843 	else if (src >= 0x08 && src <= 0x18)
844 		return 4;	/* thermistor */
845 	else if (src >= 0x20 && src <= 0x2b)
846 		return 6;	/* PECI */
847 	else if (src >= 0x42 && src <= 0x49)
848 		return 5;
849 
850 	return 0;
851 }
852 
853 static ssize_t
854 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
855 {
856 	struct nct6683_data *data = nct6683_update_device(dev);
857 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
858 	int nr = sattr->index;
859 	return sprintf(buf, "%d\n", get_temp_type(data->temp_src[nr]));
860 }
861 
862 static umode_t nct6683_temp_is_visible(struct kobject *kobj,
863 				       struct attribute *attr, int index)
864 {
865 	struct device *dev = kobj_to_dev(kobj);
866 	struct nct6683_data *data = dev_get_drvdata(dev);
867 	int temp = index / 7;	/* temp index */
868 	int nr = index % 7;	/* attribute index */
869 
870 	/*
871 	 * Intel does not have low temperature limits or temperature hysteresis
872 	 * registers, or at least register location and encoding is unknown.
873 	 */
874 	if ((nr == 2 || nr == 4) &&
875 	    data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
876 		return 0;
877 
878 	if (nr == 6 && get_temp_type(data->temp_src[temp]) == 0)
879 		return 0;				/* type */
880 
881 	return attr->mode;
882 }
883 
884 SENSOR_TEMPLATE(temp_input, "temp%d_input", S_IRUGO, show_temp16, NULL, 0);
885 SENSOR_TEMPLATE(temp_label, "temp%d_label", S_IRUGO, show_temp_label, NULL, 0);
886 SENSOR_TEMPLATE_2(temp_min, "temp%d_min", S_IRUGO, show_temp8, NULL, 0, 0);
887 SENSOR_TEMPLATE_2(temp_max, "temp%d_max", S_IRUGO, show_temp8, NULL, 0, 1);
888 SENSOR_TEMPLATE(temp_max_hyst, "temp%d_max_hyst", S_IRUGO, show_temp_hyst, NULL,
889 		0);
890 SENSOR_TEMPLATE_2(temp_crit, "temp%d_crit", S_IRUGO, show_temp8, NULL, 0, 3);
891 SENSOR_TEMPLATE(temp_type, "temp%d_type", S_IRUGO, show_temp_type, NULL, 0);
892 
893 /*
894  * nct6683_temp_is_visible uses the index into the following array
895  * to determine if attributes should be created or not.
896  * Any change in order or content must be matched.
897  */
898 static struct sensor_device_template *nct6683_attributes_temp_template[] = {
899 	&sensor_dev_template_temp_input,
900 	&sensor_dev_template_temp_label,
901 	&sensor_dev_template_temp_min,		/* 2 */
902 	&sensor_dev_template_temp_max,		/* 3 */
903 	&sensor_dev_template_temp_max_hyst,	/* 4 */
904 	&sensor_dev_template_temp_crit,		/* 5 */
905 	&sensor_dev_template_temp_type,		/* 6 */
906 	NULL
907 };
908 
909 static const struct sensor_template_group nct6683_temp_template_group = {
910 	.templates = nct6683_attributes_temp_template,
911 	.is_visible = nct6683_temp_is_visible,
912 	.base = 1,
913 };
914 
915 static ssize_t
916 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
917 {
918 	struct nct6683_data *data = nct6683_update_device(dev);
919 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
920 	int index = sattr->index;
921 
922 	return sprintf(buf, "%d\n", data->pwm[index]);
923 }
924 
925 static ssize_t
926 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
927 	  size_t count)
928 {
929 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
930 	struct nct6683_data *data = dev_get_drvdata(dev);
931 	int index = sattr->index;
932 	unsigned long val;
933 
934 	if (kstrtoul(buf, 10, &val) || val > 255)
935 		return -EINVAL;
936 
937 	mutex_lock(&data->update_lock);
938 	nct6683_write(data, NCT6683_REG_FAN_CFG_CTRL, NCT6683_FAN_CFG_REQ);
939 	usleep_range(1000, 2000);
940 	nct6683_write(data, NCT6683_REG_PWM_WRITE(index), val);
941 	nct6683_write(data, NCT6683_REG_FAN_CFG_CTRL, NCT6683_FAN_CFG_DONE);
942 	mutex_unlock(&data->update_lock);
943 
944 	return count;
945 }
946 
947 SENSOR_TEMPLATE(pwm, "pwm%d", S_IRUGO, show_pwm, store_pwm, 0);
948 
949 static umode_t nct6683_pwm_is_visible(struct kobject *kobj,
950 				      struct attribute *attr, int index)
951 {
952 	struct device *dev = kobj_to_dev(kobj);
953 	struct nct6683_data *data = dev_get_drvdata(dev);
954 	int pwm = index;	/* pwm index */
955 
956 	if (!(data->have_pwm & (1 << pwm)))
957 		return 0;
958 
959 	/* Only update pwm values for Mitac boards */
960 	if (data->customer_id == NCT6683_CUSTOMER_ID_MITAC)
961 		return attr->mode | S_IWUSR;
962 
963 	return attr->mode;
964 }
965 
966 static struct sensor_device_template *nct6683_attributes_pwm_template[] = {
967 	&sensor_dev_template_pwm,
968 	NULL
969 };
970 
971 static const struct sensor_template_group nct6683_pwm_template_group = {
972 	.templates = nct6683_attributes_pwm_template,
973 	.is_visible = nct6683_pwm_is_visible,
974 	.base = 1,
975 };
976 
977 static ssize_t
978 beep_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
979 {
980 	struct nct6683_data *data = dev_get_drvdata(dev);
981 	int ret;
982 	u8 reg;
983 
984 	mutex_lock(&data->update_lock);
985 
986 	ret = superio_enter(data->sioreg);
987 	if (ret)
988 		goto error;
989 	superio_select(data->sioreg, NCT6683_LD_HWM);
990 	reg = superio_inb(data->sioreg, NCT6683_REG_CR_BEEP);
991 	superio_exit(data->sioreg);
992 
993 	mutex_unlock(&data->update_lock);
994 
995 	return sprintf(buf, "%u\n", !!(reg & NCT6683_CR_BEEP_MASK));
996 
997 error:
998 	mutex_unlock(&data->update_lock);
999 	return ret;
1000 }
1001 
1002 static ssize_t
1003 beep_enable_store(struct device *dev, struct device_attribute *attr,
1004 		  const char *buf, size_t count)
1005 {
1006 	struct nct6683_data *data = dev_get_drvdata(dev);
1007 	unsigned long val;
1008 	u8 reg;
1009 	int ret;
1010 
1011 	if (kstrtoul(buf, 10, &val) || (val != 0 && val != 1))
1012 		return -EINVAL;
1013 
1014 	mutex_lock(&data->update_lock);
1015 
1016 	ret = superio_enter(data->sioreg);
1017 	if (ret) {
1018 		count = ret;
1019 		goto error;
1020 	}
1021 
1022 	superio_select(data->sioreg, NCT6683_LD_HWM);
1023 	reg = superio_inb(data->sioreg, NCT6683_REG_CR_BEEP);
1024 	if (val)
1025 		reg |= NCT6683_CR_BEEP_MASK;
1026 	else
1027 		reg &= ~NCT6683_CR_BEEP_MASK;
1028 	superio_outb(data->sioreg, NCT6683_REG_CR_BEEP, reg);
1029 	superio_exit(data->sioreg);
1030 error:
1031 	mutex_unlock(&data->update_lock);
1032 	return count;
1033 }
1034 
1035 /* Case open detection */
1036 
1037 static ssize_t
1038 intrusion0_alarm_show(struct device *dev, struct device_attribute *attr,
1039 		      char *buf)
1040 {
1041 	struct nct6683_data *data = dev_get_drvdata(dev);
1042 	int ret;
1043 	u8 reg;
1044 
1045 	mutex_lock(&data->update_lock);
1046 
1047 	ret = superio_enter(data->sioreg);
1048 	if (ret)
1049 		goto error;
1050 	superio_select(data->sioreg, NCT6683_LD_ACPI);
1051 	reg = superio_inb(data->sioreg, NCT6683_REG_CR_CASEOPEN);
1052 	superio_exit(data->sioreg);
1053 
1054 	mutex_unlock(&data->update_lock);
1055 
1056 	return sprintf(buf, "%u\n", !(reg & NCT6683_CR_CASEOPEN_MASK));
1057 
1058 error:
1059 	mutex_unlock(&data->update_lock);
1060 	return ret;
1061 }
1062 
1063 static ssize_t
1064 intrusion0_alarm_store(struct device *dev, struct device_attribute *attr,
1065 		       const char *buf, size_t count)
1066 {
1067 	struct nct6683_data *data = dev_get_drvdata(dev);
1068 	unsigned long val;
1069 	u8 reg;
1070 	int ret;
1071 
1072 	if (kstrtoul(buf, 10, &val) || val != 0)
1073 		return -EINVAL;
1074 
1075 	mutex_lock(&data->update_lock);
1076 
1077 	/*
1078 	 * Use CR registers to clear caseopen status.
1079 	 * Caseopen is activ low, clear by writing 1 into the register.
1080 	 */
1081 
1082 	ret = superio_enter(data->sioreg);
1083 	if (ret) {
1084 		count = ret;
1085 		goto error;
1086 	}
1087 
1088 	superio_select(data->sioreg, NCT6683_LD_ACPI);
1089 	reg = superio_inb(data->sioreg, NCT6683_REG_CR_CASEOPEN);
1090 	reg |= NCT6683_CR_CASEOPEN_MASK;
1091 	superio_outb(data->sioreg, NCT6683_REG_CR_CASEOPEN, reg);
1092 	reg &= ~NCT6683_CR_CASEOPEN_MASK;
1093 	superio_outb(data->sioreg, NCT6683_REG_CR_CASEOPEN, reg);
1094 	superio_exit(data->sioreg);
1095 
1096 	data->valid = false;	/* Force cache refresh */
1097 error:
1098 	mutex_unlock(&data->update_lock);
1099 	return count;
1100 }
1101 
1102 static DEVICE_ATTR_RW(intrusion0_alarm);
1103 static DEVICE_ATTR_RW(beep_enable);
1104 
1105 static struct attribute *nct6683_attributes_other[] = {
1106 	&dev_attr_intrusion0_alarm.attr,
1107 	&dev_attr_beep_enable.attr,
1108 	NULL
1109 };
1110 
1111 static const struct attribute_group nct6683_group_other = {
1112 	.attrs = nct6683_attributes_other,
1113 };
1114 
1115 /* Get the monitoring functions started */
1116 static inline void nct6683_init_device(struct nct6683_data *data)
1117 {
1118 	u8 tmp;
1119 
1120 	/* Start hardware monitoring if needed */
1121 	tmp = nct6683_read(data, NCT6683_HWM_CFG);
1122 	if (!(tmp & 0x80))
1123 		nct6683_write(data, NCT6683_HWM_CFG, tmp | 0x80);
1124 }
1125 
1126 /*
1127  * There are a total of 24 fan inputs. Each can be configured as input
1128  * or as output. A maximum of 16 inputs and 8 outputs is configurable.
1129  */
1130 static void
1131 nct6683_setup_fans(struct nct6683_data *data)
1132 {
1133 	int i;
1134 	u8 reg;
1135 
1136 	for (i = 0; i < NCT6683_NUM_REG_FAN; i++) {
1137 		reg = nct6683_read(data, NCT6683_REG_FANIN_CFG(i));
1138 		if (reg & 0x80)
1139 			data->have_fan |= 1 << i;
1140 		data->fanin_cfg[i] = reg;
1141 	}
1142 	for (i = 0; i < NCT6683_NUM_REG_PWM; i++) {
1143 		reg = nct6683_read(data, NCT6683_REG_FANOUT_CFG(i));
1144 		if (reg & 0x80)
1145 			data->have_pwm |= 1 << i;
1146 		data->fanout_cfg[i] = reg;
1147 	}
1148 }
1149 
1150 /*
1151  * Translation from monitoring register to temperature and voltage attributes
1152  * ==========================================================================
1153  *
1154  * There are a total of 32 monitoring registers. Each can be assigned to either
1155  * a temperature or voltage monitoring source.
1156  * NCT6683_REG_MON_CFG(x) defines assignment for each monitoring source.
1157  *
1158  * Temperature and voltage attribute mapping is determined by walking through
1159  * the NCT6683_REG_MON_CFG registers. If the assigned source is
1160  * a temperature, temp_index[n] is set to the monitor register index, and
1161  * temp_src[n] is set to the temperature source. If the assigned source is
1162  * a voltage, the respective values are stored in in_index[] and in_src[],
1163  * respectively.
1164  */
1165 
1166 static void nct6683_setup_sensors(struct nct6683_data *data)
1167 {
1168 	u8 reg;
1169 	int i;
1170 
1171 	data->temp_num = 0;
1172 	data->in_num = 0;
1173 	for (i = 0; i < NCT6683_NUM_REG_MON; i++) {
1174 		reg = nct6683_read(data, NCT6683_REG_MON_CFG(i)) & 0x7f;
1175 		/* Ignore invalid assignments */
1176 		if (reg >= NUM_MON_LABELS)
1177 			continue;
1178 		/* Skip if disabled or reserved */
1179 		if (nct6683_mon_label[reg] == NULL)
1180 			continue;
1181 		if (reg < MON_VOLTAGE_START) {
1182 			data->temp_index[data->temp_num] = i;
1183 			data->temp_src[data->temp_num] = reg;
1184 			data->temp_num++;
1185 		} else {
1186 			data->in_index[data->in_num] = i;
1187 			data->in_src[data->in_num] = reg;
1188 			data->in_num++;
1189 		}
1190 	}
1191 }
1192 
1193 static int nct6683_probe(struct platform_device *pdev)
1194 {
1195 	struct device *dev = &pdev->dev;
1196 	struct nct6683_sio_data *sio_data = dev->platform_data;
1197 	struct attribute_group *group;
1198 	struct nct6683_data *data;
1199 	struct device *hwmon_dev;
1200 	struct resource *res;
1201 	int groups = 0;
1202 	char build[16];
1203 
1204 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1205 	if (!devm_request_region(dev, res->start, IOREGION_LENGTH, DRVNAME))
1206 		return -EBUSY;
1207 
1208 	data = devm_kzalloc(dev, sizeof(struct nct6683_data), GFP_KERNEL);
1209 	if (!data)
1210 		return -ENOMEM;
1211 
1212 	data->kind = sio_data->kind;
1213 	data->sioreg = sio_data->sioreg;
1214 	data->addr = res->start;
1215 	mutex_init(&data->update_lock);
1216 	platform_set_drvdata(pdev, data);
1217 
1218 	data->customer_id = nct6683_read16(data, NCT6683_REG_CUSTOMER_ID);
1219 
1220 	/* By default only instantiate driver if the customer ID is known */
1221 	switch (data->customer_id) {
1222 	case NCT6683_CUSTOMER_ID_INTEL:
1223 		break;
1224 	case NCT6683_CUSTOMER_ID_MITAC:
1225 		break;
1226 	case NCT6683_CUSTOMER_ID_MSI:
1227 		break;
1228 	default:
1229 		if (!force)
1230 			return -ENODEV;
1231 	}
1232 
1233 	nct6683_init_device(data);
1234 	nct6683_setup_fans(data);
1235 	nct6683_setup_sensors(data);
1236 
1237 	/* Register sysfs hooks */
1238 
1239 	if (data->have_pwm) {
1240 		group = nct6683_create_attr_group(dev,
1241 						  &nct6683_pwm_template_group,
1242 						  fls(data->have_pwm));
1243 		if (IS_ERR(group))
1244 			return PTR_ERR(group);
1245 		data->groups[groups++] = group;
1246 	}
1247 
1248 	if (data->in_num) {
1249 		group = nct6683_create_attr_group(dev,
1250 						  &nct6683_in_template_group,
1251 						  data->in_num);
1252 		if (IS_ERR(group))
1253 			return PTR_ERR(group);
1254 		data->groups[groups++] = group;
1255 	}
1256 
1257 	if (data->have_fan) {
1258 		group = nct6683_create_attr_group(dev,
1259 						  &nct6683_fan_template_group,
1260 						  fls(data->have_fan));
1261 		if (IS_ERR(group))
1262 			return PTR_ERR(group);
1263 		data->groups[groups++] = group;
1264 	}
1265 
1266 	if (data->temp_num) {
1267 		group = nct6683_create_attr_group(dev,
1268 						  &nct6683_temp_template_group,
1269 						  data->temp_num);
1270 		if (IS_ERR(group))
1271 			return PTR_ERR(group);
1272 		data->groups[groups++] = group;
1273 	}
1274 	data->groups[groups++] = &nct6683_group_other;
1275 
1276 	if (data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
1277 		scnprintf(build, sizeof(build), "%02x/%02x/%02x",
1278 			  nct6683_read(data, NCT6683_REG_BUILD_MONTH),
1279 			  nct6683_read(data, NCT6683_REG_BUILD_DAY),
1280 			  nct6683_read(data, NCT6683_REG_BUILD_YEAR));
1281 	else
1282 		scnprintf(build, sizeof(build), "%02d/%02d/%02d",
1283 			  nct6683_read(data, NCT6683_REG_BUILD_MONTH),
1284 			  nct6683_read(data, NCT6683_REG_BUILD_DAY),
1285 			  nct6683_read(data, NCT6683_REG_BUILD_YEAR));
1286 
1287 	dev_info(dev, "%s EC firmware version %d.%d build %s\n",
1288 		 nct6683_chip_names[data->kind],
1289 		 nct6683_read(data, NCT6683_REG_VERSION_HI),
1290 		 nct6683_read(data, NCT6683_REG_VERSION_LO),
1291 		 build);
1292 
1293 	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
1294 			nct6683_device_names[data->kind], data, data->groups);
1295 	return PTR_ERR_OR_ZERO(hwmon_dev);
1296 }
1297 
1298 #ifdef CONFIG_PM
1299 static int nct6683_suspend(struct device *dev)
1300 {
1301 	struct nct6683_data *data = nct6683_update_device(dev);
1302 
1303 	mutex_lock(&data->update_lock);
1304 	data->hwm_cfg = nct6683_read(data, NCT6683_HWM_CFG);
1305 	mutex_unlock(&data->update_lock);
1306 
1307 	return 0;
1308 }
1309 
1310 static int nct6683_resume(struct device *dev)
1311 {
1312 	struct nct6683_data *data = dev_get_drvdata(dev);
1313 
1314 	mutex_lock(&data->update_lock);
1315 
1316 	nct6683_write(data, NCT6683_HWM_CFG, data->hwm_cfg);
1317 
1318 	/* Force re-reading all values */
1319 	data->valid = false;
1320 	mutex_unlock(&data->update_lock);
1321 
1322 	return 0;
1323 }
1324 
1325 static const struct dev_pm_ops nct6683_dev_pm_ops = {
1326 	.suspend = nct6683_suspend,
1327 	.resume = nct6683_resume,
1328 	.freeze = nct6683_suspend,
1329 	.restore = nct6683_resume,
1330 };
1331 
1332 #define NCT6683_DEV_PM_OPS	(&nct6683_dev_pm_ops)
1333 #else
1334 #define NCT6683_DEV_PM_OPS	NULL
1335 #endif /* CONFIG_PM */
1336 
1337 static struct platform_driver nct6683_driver = {
1338 	.driver = {
1339 		.name	= DRVNAME,
1340 		.pm	= NCT6683_DEV_PM_OPS,
1341 	},
1342 	.probe		= nct6683_probe,
1343 };
1344 
1345 static int __init nct6683_find(int sioaddr, struct nct6683_sio_data *sio_data)
1346 {
1347 	int addr;
1348 	u16 val;
1349 	int err;
1350 
1351 	err = superio_enter(sioaddr);
1352 	if (err)
1353 		return err;
1354 
1355 	val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
1356 	       | superio_inb(sioaddr, SIO_REG_DEVID + 1);
1357 
1358 	switch (val & SIO_ID_MASK) {
1359 	case SIO_NCT6683_ID:
1360 		sio_data->kind = nct6683;
1361 		break;
1362 	case SIO_NCT6687_ID:
1363 		sio_data->kind = nct6687;
1364 		break;
1365 	default:
1366 		if (val != 0xffff)
1367 			pr_debug("unsupported chip ID: 0x%04x\n", val);
1368 		goto fail;
1369 	}
1370 
1371 	/* We have a known chip, find the HWM I/O address */
1372 	superio_select(sioaddr, NCT6683_LD_HWM);
1373 	val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
1374 	    | superio_inb(sioaddr, SIO_REG_ADDR + 1);
1375 	addr = val & IOREGION_ALIGNMENT;
1376 	if (addr == 0) {
1377 		pr_err("EC base I/O port unconfigured\n");
1378 		goto fail;
1379 	}
1380 
1381 	/* Activate logical device if needed */
1382 	val = superio_inb(sioaddr, SIO_REG_ENABLE);
1383 	if (!(val & 0x01)) {
1384 		pr_warn("Forcibly enabling EC access. Data may be unusable.\n");
1385 		superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
1386 	}
1387 
1388 	superio_exit(sioaddr);
1389 	pr_info("Found %s or compatible chip at %#x:%#x\n",
1390 		nct6683_chip_names[sio_data->kind], sioaddr, addr);
1391 	sio_data->sioreg = sioaddr;
1392 
1393 	return addr;
1394 
1395 fail:
1396 	superio_exit(sioaddr);
1397 	return -ENODEV;
1398 }
1399 
1400 /*
1401  * when Super-I/O functions move to a separate file, the Super-I/O
1402  * bus will manage the lifetime of the device and this module will only keep
1403  * track of the nct6683 driver. But since we use platform_device_alloc(), we
1404  * must keep track of the device
1405  */
1406 static struct platform_device *pdev[2];
1407 
1408 static int __init sensors_nct6683_init(void)
1409 {
1410 	struct nct6683_sio_data sio_data;
1411 	int sioaddr[2] = { 0x2e, 0x4e };
1412 	struct resource res;
1413 	bool found = false;
1414 	int address;
1415 	int i, err;
1416 
1417 	err = platform_driver_register(&nct6683_driver);
1418 	if (err)
1419 		return err;
1420 
1421 	/*
1422 	 * initialize sio_data->kind and sio_data->sioreg.
1423 	 *
1424 	 * when Super-I/O functions move to a separate file, the Super-I/O
1425 	 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
1426 	 * nct6683 hardware monitor, and call probe()
1427 	 */
1428 	for (i = 0; i < ARRAY_SIZE(pdev); i++) {
1429 		address = nct6683_find(sioaddr[i], &sio_data);
1430 		if (address <= 0)
1431 			continue;
1432 
1433 		found = true;
1434 
1435 		pdev[i] = platform_device_alloc(DRVNAME, address);
1436 		if (!pdev[i]) {
1437 			err = -ENOMEM;
1438 			goto exit_device_unregister;
1439 		}
1440 
1441 		err = platform_device_add_data(pdev[i], &sio_data,
1442 					       sizeof(struct nct6683_sio_data));
1443 		if (err)
1444 			goto exit_device_put;
1445 
1446 		memset(&res, 0, sizeof(res));
1447 		res.name = DRVNAME;
1448 		res.start = address + IOREGION_OFFSET;
1449 		res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
1450 		res.flags = IORESOURCE_IO;
1451 
1452 		err = acpi_check_resource_conflict(&res);
1453 		if (err) {
1454 			platform_device_put(pdev[i]);
1455 			pdev[i] = NULL;
1456 			continue;
1457 		}
1458 
1459 		err = platform_device_add_resources(pdev[i], &res, 1);
1460 		if (err)
1461 			goto exit_device_put;
1462 
1463 		/* platform_device_add calls probe() */
1464 		err = platform_device_add(pdev[i]);
1465 		if (err)
1466 			goto exit_device_put;
1467 	}
1468 	if (!found) {
1469 		err = -ENODEV;
1470 		goto exit_unregister;
1471 	}
1472 
1473 	return 0;
1474 
1475 exit_device_put:
1476 	platform_device_put(pdev[i]);
1477 exit_device_unregister:
1478 	while (--i >= 0) {
1479 		if (pdev[i])
1480 			platform_device_unregister(pdev[i]);
1481 	}
1482 exit_unregister:
1483 	platform_driver_unregister(&nct6683_driver);
1484 	return err;
1485 }
1486 
1487 static void __exit sensors_nct6683_exit(void)
1488 {
1489 	int i;
1490 
1491 	for (i = 0; i < ARRAY_SIZE(pdev); i++) {
1492 		if (pdev[i])
1493 			platform_device_unregister(pdev[i]);
1494 	}
1495 	platform_driver_unregister(&nct6683_driver);
1496 }
1497 
1498 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
1499 MODULE_DESCRIPTION("NCT6683D driver");
1500 MODULE_LICENSE("GPL");
1501 
1502 module_init(sensors_nct6683_init);
1503 module_exit(sensors_nct6683_exit);
1504