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