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