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 <[email protected]>
7  *
8  * Derived from nct6775 driver
9  * Copyright (C) 2012, 2013  Guenter Roeck <[email protected]>
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
superio_outb(int ioreg,int reg,int val)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
superio_inb(int ioreg,int reg)84 superio_inb(int ioreg, int reg)
85 {
86 	outb(reg, ioreg);
87 	return inb(ioreg + 1);
88 }
89 
90 static inline void
superio_select(int ioreg,int ld)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
superio_enter(int ioreg)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
superio_exit(int ioreg)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_ASROCK		0xe2c
180 #define NCT6683_CUSTOMER_ID_ASROCK2	0xe1b
181 #define NCT6683_CUSTOMER_ID_ASROCK3	0x1631
182 #define NCT6683_CUSTOMER_ID_ASROCK4	0x163e
183 
184 #define NCT6683_REG_BUILD_YEAR		0x604
185 #define NCT6683_REG_BUILD_MONTH		0x605
186 #define NCT6683_REG_BUILD_DAY		0x606
187 #define NCT6683_REG_SERIAL		0x607
188 #define NCT6683_REG_VERSION_HI		0x608
189 #define NCT6683_REG_VERSION_LO		0x609
190 
191 #define NCT6683_REG_CR_CASEOPEN		0xe8
192 #define NCT6683_CR_CASEOPEN_MASK	(1 << 7)
193 
194 #define NCT6683_REG_CR_BEEP		0xe0
195 #define NCT6683_CR_BEEP_MASK		(1 << 6)
196 
197 static const char *const nct6683_mon_label[] = {
198 	NULL,	/* disabled */
199 	"Local",
200 	"Diode 0 (curr)",
201 	"Diode 1 (curr)",
202 	"Diode 2 (curr)",
203 	"Diode 0 (volt)",
204 	"Diode 1 (volt)",
205 	"Diode 2 (volt)",
206 	"Thermistor 14",
207 	"Thermistor 15",
208 	"Thermistor 16",
209 	"Thermistor 0",
210 	"Thermistor 1",
211 	"Thermistor 2",
212 	"Thermistor 3",
213 	"Thermistor 4",
214 	"Thermistor 5",		/* 0x10 */
215 	"Thermistor 6",
216 	"Thermistor 7",
217 	"Thermistor 8",
218 	"Thermistor 9",
219 	"Thermistor 10",
220 	"Thermistor 11",
221 	"Thermistor 12",
222 	"Thermistor 13",
223 	NULL, NULL, NULL, NULL, NULL, NULL, NULL,
224 	"PECI 0.0",		/* 0x20 */
225 	"PECI 1.0",
226 	"PECI 2.0",
227 	"PECI 3.0",
228 	"PECI 0.1",
229 	"PECI 1.1",
230 	"PECI 2.1",
231 	"PECI 3.1",
232 	"PECI DIMM 0",
233 	"PECI DIMM 1",
234 	"PECI DIMM 2",
235 	"PECI DIMM 3",
236 	NULL, NULL, NULL, NULL,
237 	"PCH CPU",		/* 0x30 */
238 	"PCH CHIP",
239 	"PCH CHIP CPU MAX",
240 	"PCH MCH",
241 	"PCH DIMM 0",
242 	"PCH DIMM 1",
243 	"PCH DIMM 2",
244 	"PCH DIMM 3",
245 	"SMBus 0",
246 	"SMBus 1",
247 	"SMBus 2",
248 	"SMBus 3",
249 	"SMBus 4",
250 	"SMBus 5",
251 	"DIMM 0",
252 	"DIMM 1",
253 	"DIMM 2",		/* 0x40 */
254 	"DIMM 3",
255 	"AMD TSI Addr 90h",
256 	"AMD TSI Addr 92h",
257 	"AMD TSI Addr 94h",
258 	"AMD TSI Addr 96h",
259 	"AMD TSI Addr 98h",
260 	"AMD TSI Addr 9ah",
261 	"AMD TSI Addr 9ch",
262 	"AMD TSI Addr 9dh",
263 	NULL, NULL, NULL, NULL, NULL, NULL,
264 	"Virtual 0",		/* 0x50 */
265 	"Virtual 1",
266 	"Virtual 2",
267 	"Virtual 3",
268 	"Virtual 4",
269 	"Virtual 5",
270 	"Virtual 6",
271 	"Virtual 7",
272 	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
273 	"VCC",			/* 0x60 voltage sensors */
274 	"VSB",
275 	"AVSB",
276 	"VTT",
277 	"VBAT",
278 	"VREF",
279 	"VIN0",
280 	"VIN1",
281 	"VIN2",
282 	"VIN3",
283 	"VIN4",
284 	"VIN5",
285 	"VIN6",
286 	"VIN7",
287 	"VIN8",
288 	"VIN9",
289 	"VIN10",
290 	"VIN11",
291 	"VIN12",
292 	"VIN13",
293 	"VIN14",
294 	"VIN15",
295 	"VIN16",
296 };
297 
298 #define NUM_MON_LABELS		ARRAY_SIZE(nct6683_mon_label)
299 #define MON_VOLTAGE_START	0x60
300 
301 /* ------------------------------------------------------- */
302 
303 struct nct6683_data {
304 	int addr;		/* IO base of EC space */
305 	int sioreg;		/* SIO register */
306 	enum kinds kind;
307 	u16 customer_id;
308 
309 	struct device *hwmon_dev;
310 	const struct attribute_group *groups[6];
311 
312 	int temp_num;			/* number of temperature attributes */
313 	u8 temp_index[NCT6683_NUM_REG_MON];
314 	u8 temp_src[NCT6683_NUM_REG_MON];
315 
316 	u8 in_num;			/* number of voltage attributes */
317 	u8 in_index[NCT6683_NUM_REG_MON];
318 	u8 in_src[NCT6683_NUM_REG_MON];
319 
320 	struct mutex update_lock;	/* used to protect sensor updates */
321 	bool valid;			/* true if following fields are valid */
322 	unsigned long last_updated;	/* In jiffies */
323 
324 	/* Voltage attribute values */
325 	u8 in[3][NCT6683_NUM_REG_MON];	/* [0]=in, [1]=in_max, [2]=in_min */
326 
327 	/* Temperature attribute values */
328 	s16 temp_in[NCT6683_NUM_REG_MON];
329 	s8 temp[4][NCT6683_NUM_REG_MON];/* [0]=min, [1]=max, [2]=hyst,
330 					 * [3]=crit
331 					 */
332 
333 	/* Fan attribute values */
334 	unsigned int rpm[NCT6683_NUM_REG_FAN];
335 	u16 fan_min[NCT6683_NUM_REG_FAN];
336 	u8 fanin_cfg[NCT6683_NUM_REG_FAN];
337 	u8 fanout_cfg[NCT6683_NUM_REG_FAN];
338 	u16 have_fan;			/* some fan inputs can be disabled */
339 
340 	u8 have_pwm;
341 	u8 pwm[NCT6683_NUM_REG_PWM];
342 
343 #ifdef CONFIG_PM
344 	/* Remember extra register values over suspend/resume */
345 	u8 hwm_cfg;
346 #endif
347 };
348 
349 struct nct6683_sio_data {
350 	int sioreg;
351 	enum kinds kind;
352 };
353 
354 struct sensor_device_template {
355 	struct device_attribute dev_attr;
356 	union {
357 		struct {
358 			u8 nr;
359 			u8 index;
360 		} s;
361 		int index;
362 	} u;
363 	bool s2;	/* true if both index and nr are used */
364 };
365 
366 struct sensor_device_attr_u {
367 	union {
368 		struct sensor_device_attribute a1;
369 		struct sensor_device_attribute_2 a2;
370 	} u;
371 	char name[32];
372 };
373 
374 #define __TEMPLATE_ATTR(_template, _mode, _show, _store) {	\
375 	.attr = {.name = _template, .mode = _mode },		\
376 	.show	= _show,					\
377 	.store	= _store,					\
378 }
379 
380 #define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index)	\
381 	{ .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store),	\
382 	  .u.index = _index,						\
383 	  .s2 = false }
384 
385 #define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store,	\
386 				 _nr, _index)				\
387 	{ .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store),	\
388 	  .u.s.index = _index,						\
389 	  .u.s.nr = _nr,						\
390 	  .s2 = true }
391 
392 #define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index)	\
393 static struct sensor_device_template sensor_dev_template_##_name	\
394 	= SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store,	\
395 				 _index)
396 
397 #define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store,	\
398 			  _nr, _index)					\
399 static struct sensor_device_template sensor_dev_template_##_name	\
400 	= SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store,	\
401 				 _nr, _index)
402 
403 struct sensor_template_group {
404 	struct sensor_device_template **templates;
405 	umode_t (*is_visible)(struct kobject *, struct attribute *, int);
406 	int base;
407 };
408 
409 static struct attribute_group *
nct6683_create_attr_group(struct device * dev,const struct sensor_template_group * tg,int repeat)410 nct6683_create_attr_group(struct device *dev,
411 			  const struct sensor_template_group *tg,
412 			  int repeat)
413 {
414 	struct sensor_device_attribute_2 *a2;
415 	struct sensor_device_attribute *a;
416 	struct sensor_device_template **t;
417 	struct sensor_device_attr_u *su;
418 	struct attribute_group *group;
419 	struct attribute **attrs;
420 	int i, count;
421 
422 	if (repeat <= 0)
423 		return ERR_PTR(-EINVAL);
424 
425 	t = tg->templates;
426 	for (count = 0; *t; t++, count++)
427 		;
428 
429 	if (count == 0)
430 		return ERR_PTR(-EINVAL);
431 
432 	group = devm_kzalloc(dev, sizeof(*group), GFP_KERNEL);
433 	if (group == NULL)
434 		return ERR_PTR(-ENOMEM);
435 
436 	attrs = devm_kcalloc(dev, repeat * count + 1, sizeof(*attrs),
437 			     GFP_KERNEL);
438 	if (attrs == NULL)
439 		return ERR_PTR(-ENOMEM);
440 
441 	su = devm_kzalloc(dev, array3_size(repeat, count, sizeof(*su)),
442 			  GFP_KERNEL);
443 	if (su == NULL)
444 		return ERR_PTR(-ENOMEM);
445 
446 	group->attrs = attrs;
447 	group->is_visible = tg->is_visible;
448 
449 	for (i = 0; i < repeat; i++) {
450 		t = tg->templates;
451 		while (*t) {
452 			snprintf(su->name, sizeof(su->name),
453 				 (*t)->dev_attr.attr.name, tg->base + i);
454 			if ((*t)->s2) {
455 				a2 = &su->u.a2;
456 				sysfs_attr_init(&a2->dev_attr.attr);
457 				a2->dev_attr.attr.name = su->name;
458 				a2->nr = (*t)->u.s.nr + i;
459 				a2->index = (*t)->u.s.index;
460 				a2->dev_attr.attr.mode =
461 				  (*t)->dev_attr.attr.mode;
462 				a2->dev_attr.show = (*t)->dev_attr.show;
463 				a2->dev_attr.store = (*t)->dev_attr.store;
464 				*attrs = &a2->dev_attr.attr;
465 			} else {
466 				a = &su->u.a1;
467 				sysfs_attr_init(&a->dev_attr.attr);
468 				a->dev_attr.attr.name = su->name;
469 				a->index = (*t)->u.index + i;
470 				a->dev_attr.attr.mode =
471 				  (*t)->dev_attr.attr.mode;
472 				a->dev_attr.show = (*t)->dev_attr.show;
473 				a->dev_attr.store = (*t)->dev_attr.store;
474 				*attrs = &a->dev_attr.attr;
475 			}
476 			attrs++;
477 			su++;
478 			t++;
479 		}
480 	}
481 
482 	return group;
483 }
484 
485 /* LSB is 16 mV, except for the following sources, where it is 32 mV */
486 #define MON_SRC_VCC	0x60
487 #define MON_SRC_VSB	0x61
488 #define MON_SRC_AVSB	0x62
489 #define MON_SRC_VBAT	0x64
490 
in_from_reg(u16 reg,u8 src)491 static inline long in_from_reg(u16 reg, 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 	return reg * scale;
499 }
500 
nct6683_read(struct nct6683_data * data,u16 reg)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 
nct6683_read16(struct nct6683_data * data,u16 reg)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 
nct6683_write(struct nct6683_data * data,u16 reg,u16 value)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 
get_in_reg(struct nct6683_data * data,int nr,int index)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 
get_temp_reg(struct nct6683_data * data,int nr,int index)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 
nct6683_update_pwm(struct device * dev)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 
nct6683_update_device(struct device * dev)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
show_in_label(struct device * dev,struct device_attribute * attr,char * buf)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
show_in_reg(struct device * dev,struct device_attribute * attr,char * buf)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 
nct6683_in_is_visible(struct kobject * kobj,struct attribute * attr,int index)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
show_fan(struct device * dev,struct device_attribute * attr,char * buf)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
show_fan_min(struct device * dev,struct device_attribute * attr,char * buf)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
show_fan_pulses(struct device * dev,struct device_attribute * attr,char * buf)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 
nct6683_fan_is_visible(struct kobject * kobj,struct attribute * attr,int index)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
show_temp_label(struct device * dev,struct device_attribute * attr,char * buf)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
show_temp8(struct device * dev,struct device_attribute * attr,char * buf)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
show_temp_hyst(struct device * dev,struct device_attribute * attr,char * buf)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
show_temp16(struct device * dev,struct device_attribute * attr,char * buf)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 
get_temp_type(u8 src)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
show_temp_type(struct device * dev,struct device_attribute * attr,char * buf)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 
nct6683_temp_is_visible(struct kobject * kobj,struct attribute * attr,int index)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
show_pwm(struct device * dev,struct device_attribute * attr,char * buf)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
store_pwm(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)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 
nct6683_pwm_is_visible(struct kobject * kobj,struct attribute * attr,int index)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
beep_enable_show(struct device * dev,struct device_attribute * attr,char * buf)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
beep_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)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
intrusion0_alarm_show(struct device * dev,struct device_attribute * attr,char * buf)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
intrusion0_alarm_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)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 */
nct6683_init_device(struct nct6683_data * data)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
nct6683_setup_fans(struct nct6683_data * data)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 
nct6683_setup_sensors(struct nct6683_data * data)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 
nct6683_probe(struct platform_device * pdev)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 	case NCT6683_CUSTOMER_ID_MSI2:
1229 		break;
1230 	case NCT6683_CUSTOMER_ID_MSI3:
1231 		break;
1232 	case NCT6683_CUSTOMER_ID_MSI4:
1233 		break;
1234 	case NCT6683_CUSTOMER_ID_ASROCK:
1235 		break;
1236 	case NCT6683_CUSTOMER_ID_ASROCK2:
1237 		break;
1238 	case NCT6683_CUSTOMER_ID_ASROCK3:
1239 		break;
1240 	case NCT6683_CUSTOMER_ID_ASROCK4:
1241 		break;
1242 	default:
1243 		if (!force)
1244 			return -ENODEV;
1245 		dev_warn(dev, "Enabling support for unknown customer ID 0x%04x\n", data->customer_id);
1246 		break;
1247 	}
1248 
1249 	nct6683_init_device(data);
1250 	nct6683_setup_fans(data);
1251 	nct6683_setup_sensors(data);
1252 
1253 	/* Register sysfs hooks */
1254 
1255 	if (data->have_pwm) {
1256 		group = nct6683_create_attr_group(dev,
1257 						  &nct6683_pwm_template_group,
1258 						  fls(data->have_pwm));
1259 		if (IS_ERR(group))
1260 			return PTR_ERR(group);
1261 		data->groups[groups++] = group;
1262 	}
1263 
1264 	if (data->in_num) {
1265 		group = nct6683_create_attr_group(dev,
1266 						  &nct6683_in_template_group,
1267 						  data->in_num);
1268 		if (IS_ERR(group))
1269 			return PTR_ERR(group);
1270 		data->groups[groups++] = group;
1271 	}
1272 
1273 	if (data->have_fan) {
1274 		group = nct6683_create_attr_group(dev,
1275 						  &nct6683_fan_template_group,
1276 						  fls(data->have_fan));
1277 		if (IS_ERR(group))
1278 			return PTR_ERR(group);
1279 		data->groups[groups++] = group;
1280 	}
1281 
1282 	if (data->temp_num) {
1283 		group = nct6683_create_attr_group(dev,
1284 						  &nct6683_temp_template_group,
1285 						  data->temp_num);
1286 		if (IS_ERR(group))
1287 			return PTR_ERR(group);
1288 		data->groups[groups++] = group;
1289 	}
1290 	data->groups[groups++] = &nct6683_group_other;
1291 
1292 	if (data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
1293 		scnprintf(build, sizeof(build), "%02x/%02x/%02x",
1294 			  nct6683_read(data, NCT6683_REG_BUILD_MONTH),
1295 			  nct6683_read(data, NCT6683_REG_BUILD_DAY),
1296 			  nct6683_read(data, NCT6683_REG_BUILD_YEAR));
1297 	else
1298 		scnprintf(build, sizeof(build), "%02d/%02d/%02d",
1299 			  nct6683_read(data, NCT6683_REG_BUILD_MONTH),
1300 			  nct6683_read(data, NCT6683_REG_BUILD_DAY),
1301 			  nct6683_read(data, NCT6683_REG_BUILD_YEAR));
1302 
1303 	dev_info(dev, "%s EC firmware version %d.%d build %s\n",
1304 		 nct6683_chip_names[data->kind],
1305 		 nct6683_read(data, NCT6683_REG_VERSION_HI),
1306 		 nct6683_read(data, NCT6683_REG_VERSION_LO),
1307 		 build);
1308 
1309 	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
1310 			nct6683_device_names[data->kind], data, data->groups);
1311 	return PTR_ERR_OR_ZERO(hwmon_dev);
1312 }
1313 
1314 #ifdef CONFIG_PM
nct6683_suspend(struct device * dev)1315 static int nct6683_suspend(struct device *dev)
1316 {
1317 	struct nct6683_data *data = nct6683_update_device(dev);
1318 
1319 	mutex_lock(&data->update_lock);
1320 	data->hwm_cfg = nct6683_read(data, NCT6683_HWM_CFG);
1321 	mutex_unlock(&data->update_lock);
1322 
1323 	return 0;
1324 }
1325 
nct6683_resume(struct device * dev)1326 static int nct6683_resume(struct device *dev)
1327 {
1328 	struct nct6683_data *data = dev_get_drvdata(dev);
1329 
1330 	mutex_lock(&data->update_lock);
1331 
1332 	nct6683_write(data, NCT6683_HWM_CFG, data->hwm_cfg);
1333 
1334 	/* Force re-reading all values */
1335 	data->valid = false;
1336 	mutex_unlock(&data->update_lock);
1337 
1338 	return 0;
1339 }
1340 
1341 static const struct dev_pm_ops nct6683_dev_pm_ops = {
1342 	.suspend = nct6683_suspend,
1343 	.resume = nct6683_resume,
1344 	.freeze = nct6683_suspend,
1345 	.restore = nct6683_resume,
1346 };
1347 
1348 #define NCT6683_DEV_PM_OPS	(&nct6683_dev_pm_ops)
1349 #else
1350 #define NCT6683_DEV_PM_OPS	NULL
1351 #endif /* CONFIG_PM */
1352 
1353 static struct platform_driver nct6683_driver = {
1354 	.driver = {
1355 		.name	= DRVNAME,
1356 		.pm	= NCT6683_DEV_PM_OPS,
1357 	},
1358 	.probe		= nct6683_probe,
1359 };
1360 
nct6683_find(int sioaddr,struct nct6683_sio_data * sio_data)1361 static int __init nct6683_find(int sioaddr, struct nct6683_sio_data *sio_data)
1362 {
1363 	int addr;
1364 	u16 val;
1365 	int err;
1366 
1367 	err = superio_enter(sioaddr);
1368 	if (err)
1369 		return err;
1370 
1371 	val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
1372 	       | superio_inb(sioaddr, SIO_REG_DEVID + 1);
1373 
1374 	switch (val & SIO_ID_MASK) {
1375 	case SIO_NCT6683_ID:
1376 		sio_data->kind = nct6683;
1377 		break;
1378 	case SIO_NCT6686_ID:
1379 		sio_data->kind = nct6686;
1380 		break;
1381 	case SIO_NCT6687_ID:
1382 		sio_data->kind = nct6687;
1383 		break;
1384 	default:
1385 		if (val != 0xffff)
1386 			pr_debug("unsupported chip ID: 0x%04x\n", val);
1387 		goto fail;
1388 	}
1389 
1390 	/* We have a known chip, find the HWM I/O address */
1391 	superio_select(sioaddr, NCT6683_LD_HWM);
1392 	val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
1393 	    | superio_inb(sioaddr, SIO_REG_ADDR + 1);
1394 	addr = val & IOREGION_ALIGNMENT;
1395 	if (addr == 0) {
1396 		pr_err("EC base I/O port unconfigured\n");
1397 		goto fail;
1398 	}
1399 
1400 	/* Activate logical device if needed */
1401 	val = superio_inb(sioaddr, SIO_REG_ENABLE);
1402 	if (!(val & 0x01)) {
1403 		pr_warn("Forcibly enabling EC access. Data may be unusable.\n");
1404 		superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
1405 	}
1406 
1407 	superio_exit(sioaddr);
1408 	pr_info("Found %s or compatible chip at %#x:%#x\n",
1409 		nct6683_chip_names[sio_data->kind], sioaddr, addr);
1410 	sio_data->sioreg = sioaddr;
1411 
1412 	return addr;
1413 
1414 fail:
1415 	superio_exit(sioaddr);
1416 	return -ENODEV;
1417 }
1418 
1419 /*
1420  * when Super-I/O functions move to a separate file, the Super-I/O
1421  * bus will manage the lifetime of the device and this module will only keep
1422  * track of the nct6683 driver. But since we use platform_device_alloc(), we
1423  * must keep track of the device
1424  */
1425 static struct platform_device *pdev[2];
1426 
sensors_nct6683_init(void)1427 static int __init sensors_nct6683_init(void)
1428 {
1429 	struct nct6683_sio_data sio_data;
1430 	int sioaddr[2] = { 0x2e, 0x4e };
1431 	struct resource res;
1432 	bool found = false;
1433 	int address;
1434 	int i, err;
1435 
1436 	err = platform_driver_register(&nct6683_driver);
1437 	if (err)
1438 		return err;
1439 
1440 	/*
1441 	 * initialize sio_data->kind and sio_data->sioreg.
1442 	 *
1443 	 * when Super-I/O functions move to a separate file, the Super-I/O
1444 	 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
1445 	 * nct6683 hardware monitor, and call probe()
1446 	 */
1447 	for (i = 0; i < ARRAY_SIZE(pdev); i++) {
1448 		address = nct6683_find(sioaddr[i], &sio_data);
1449 		if (address <= 0)
1450 			continue;
1451 
1452 		found = true;
1453 
1454 		pdev[i] = platform_device_alloc(DRVNAME, address);
1455 		if (!pdev[i]) {
1456 			err = -ENOMEM;
1457 			goto exit_device_unregister;
1458 		}
1459 
1460 		err = platform_device_add_data(pdev[i], &sio_data,
1461 					       sizeof(struct nct6683_sio_data));
1462 		if (err)
1463 			goto exit_device_put;
1464 
1465 		memset(&res, 0, sizeof(res));
1466 		res.name = DRVNAME;
1467 		res.start = address + IOREGION_OFFSET;
1468 		res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
1469 		res.flags = IORESOURCE_IO;
1470 
1471 		err = acpi_check_resource_conflict(&res);
1472 		if (err) {
1473 			platform_device_put(pdev[i]);
1474 			pdev[i] = NULL;
1475 			continue;
1476 		}
1477 
1478 		err = platform_device_add_resources(pdev[i], &res, 1);
1479 		if (err)
1480 			goto exit_device_put;
1481 
1482 		/* platform_device_add calls probe() */
1483 		err = platform_device_add(pdev[i]);
1484 		if (err)
1485 			goto exit_device_put;
1486 	}
1487 	if (!found) {
1488 		err = -ENODEV;
1489 		goto exit_unregister;
1490 	}
1491 
1492 	return 0;
1493 
1494 exit_device_put:
1495 	platform_device_put(pdev[i]);
1496 exit_device_unregister:
1497 	while (--i >= 0) {
1498 		if (pdev[i])
1499 			platform_device_unregister(pdev[i]);
1500 	}
1501 exit_unregister:
1502 	platform_driver_unregister(&nct6683_driver);
1503 	return err;
1504 }
1505 
sensors_nct6683_exit(void)1506 static void __exit sensors_nct6683_exit(void)
1507 {
1508 	int i;
1509 
1510 	for (i = 0; i < ARRAY_SIZE(pdev); i++) {
1511 		if (pdev[i])
1512 			platform_device_unregister(pdev[i]);
1513 	}
1514 	platform_driver_unregister(&nct6683_driver);
1515 }
1516 
1517 MODULE_AUTHOR("Guenter Roeck <[email protected]>");
1518 MODULE_DESCRIPTION("NCT6683D driver");
1519 MODULE_LICENSE("GPL");
1520 
1521 module_init(sensors_nct6683_init);
1522 module_exit(sensors_nct6683_exit);
1523