Linux Audio

Check our new training course

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