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