hwmon.c 25.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3 4 5 6 7 8
 * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
 *
 * This file defines the sysfs class "hwmon", for use by sensors drivers.
 *
 * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
 */
9

10 11
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

12
#include <linux/bitops.h>
13 14
#include <linux/device.h>
#include <linux/err.h>
15
#include <linux/gfp.h>
16 17 18
#include <linux/hwmon.h>
#include <linux/idr.h>
#include <linux/module.h>
19
#include <linux/pci.h>
20
#include <linux/slab.h>
21
#include <linux/string.h>
22
#include <linux/thermal.h>
23

24 25 26
#define CREATE_TRACE_POINTS
#include <trace/events/hwmon.h>

27 28 29
#define HWMON_ID_PREFIX "hwmon"
#define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"

30 31 32
struct hwmon_device {
	const char *name;
	struct device dev;
33 34 35 36
	const struct hwmon_chip_info *chip;

	struct attribute_group group;
	const struct attribute_group **groups;
37
};
38

39 40
#define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)

41 42
#define MAX_SYSFS_ATTR_NAME_LENGTH	32

43 44 45 46 47 48
struct hwmon_device_attribute {
	struct device_attribute dev_attr;
	const struct hwmon_ops *ops;
	enum hwmon_sensor_types type;
	u32 attr;
	int index;
49
	char name[MAX_SYSFS_ATTR_NAME_LENGTH];
50 51 52 53
};

#define to_hwmon_attr(d) \
	container_of(d, struct hwmon_device_attribute, dev_attr)
54
#define to_dev_attr(a) container_of(a, struct device_attribute, attr)
55 56 57 58 59 60 61

/*
 * Thermal zone information
 * In addition to the reference to the hwmon device,
 * also provides the sensor index.
 */
struct hwmon_thermal_data {
62
	struct device *dev;		/* Reference to hwmon device */
63 64 65
	int index;			/* sensor index */
};

66
static ssize_t
67
name_show(struct device *dev, struct device_attribute *attr, char *buf)
68 69 70
{
	return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
}
71
static DEVICE_ATTR_RO(name);
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

static struct attribute *hwmon_dev_attrs[] = {
	&dev_attr_name.attr,
	NULL
};

static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
					 struct attribute *attr, int n)
{
	struct device *dev = container_of(kobj, struct device, kobj);

	if (to_hwmon_device(dev)->name == NULL)
		return 0;

	return attr->mode;
}

89
static const struct attribute_group hwmon_dev_attr_group = {
90 91 92 93 94 95 96 97 98
	.attrs		= hwmon_dev_attrs,
	.is_visible	= hwmon_dev_name_is_visible,
};

static const struct attribute_group *hwmon_dev_attr_groups[] = {
	&hwmon_dev_attr_group,
	NULL
};

99 100 101 102 103 104 105 106 107 108 109 110 111
static void hwmon_free_attrs(struct attribute **attrs)
{
	int i;

	for (i = 0; attrs[i]; i++) {
		struct device_attribute *dattr = to_dev_attr(attrs[i]);
		struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr);

		kfree(hattr);
	}
	kfree(attrs);
}

112 113
static void hwmon_dev_release(struct device *dev)
{
114 115 116 117 118 119
	struct hwmon_device *hwdev = to_hwmon_device(dev);

	if (hwdev->group.attrs)
		hwmon_free_attrs(hwdev->group.attrs);
	kfree(hwdev->groups);
	kfree(hwdev);
120 121 122 123 124 125 126 127
}

static struct class hwmon_class = {
	.name = "hwmon",
	.owner = THIS_MODULE,
	.dev_groups = hwmon_dev_attr_groups,
	.dev_release = hwmon_dev_release,
};
128

129
static DEFINE_IDA(hwmon_ida);
130

131 132
/* Thermal zone handling */

133 134 135 136
/*
 * The complex conditional is necessary to avoid a cyclic dependency
 * between hwmon and thermal_sys modules.
 */
137
#ifdef CONFIG_THERMAL_OF
138 139 140
static int hwmon_thermal_get_temp(void *data, int *temp)
{
	struct hwmon_thermal_data *tdata = data;
141
	struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
142 143 144
	int ret;
	long t;

145
	ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
146 147 148 149 150 151 152 153 154
				     tdata->index, &t);
	if (ret < 0)
		return ret;

	*temp = t;

	return 0;
}

155
static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
156 157 158
	.get_temp = hwmon_thermal_get_temp,
};

159
static int hwmon_thermal_add_sensor(struct device *dev, int index)
160 161
{
	struct hwmon_thermal_data *tdata;
162
	struct thermal_zone_device *tzd;
163 164 165 166 167

	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
	if (!tdata)
		return -ENOMEM;

168
	tdata->dev = dev;
169 170
	tdata->index = index;

171
	tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata,
172 173 174 175 176 177 178
						   &hwmon_thermal_ops);
	/*
	 * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV,
	 * so ignore that error but forward any other error.
	 */
	if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV))
		return PTR_ERR(tzd);
179 180 181 182

	return 0;
}
#else
183
static int hwmon_thermal_add_sensor(struct device *dev, int index)
184 185 186
{
	return 0;
}
187
#endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
188

189 190
static int hwmon_attr_base(enum hwmon_sensor_types type)
{
191
	if (type == hwmon_in || type == hwmon_intrusion)
192 193 194 195
		return 0;
	return 1;
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209
/* sysfs attribute management */

static ssize_t hwmon_attr_show(struct device *dev,
			       struct device_attribute *devattr, char *buf)
{
	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
	long val;
	int ret;

	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
			       &val);
	if (ret < 0)
		return ret;

210 211 212
	trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
			      hattr->name, val);

213 214 215
	return sprintf(buf, "%ld\n", val);
}

216 217 218 219 220
static ssize_t hwmon_attr_show_string(struct device *dev,
				      struct device_attribute *devattr,
				      char *buf)
{
	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
221
	enum hwmon_sensor_types type = hattr->type;
222
	const char *s;
223 224 225 226 227 228 229
	int ret;

	ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
				      hattr->index, &s);
	if (ret < 0)
		return ret;

230 231 232
	trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
				     hattr->name, s);

233 234 235
	return sprintf(buf, "%s\n", s);
}

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
static ssize_t hwmon_attr_store(struct device *dev,
				struct device_attribute *devattr,
				const char *buf, size_t count)
{
	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
	long val;
	int ret;

	ret = kstrtol(buf, 10, &val);
	if (ret < 0)
		return ret;

	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
				val);
	if (ret < 0)
		return ret;

253 254
	trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
			       hattr->name, val);
255

256
	return count;
257 258
}

259 260 261 262 263 264 265 266 267 268 269
static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
{
	return (type == hwmon_temp && attr == hwmon_temp_label) ||
	       (type == hwmon_in && attr == hwmon_in_label) ||
	       (type == hwmon_curr && attr == hwmon_curr_label) ||
	       (type == hwmon_power && attr == hwmon_power_label) ||
	       (type == hwmon_energy && attr == hwmon_energy_label) ||
	       (type == hwmon_humidity && attr == hwmon_humidity_label) ||
	       (type == hwmon_fan && attr == hwmon_fan_label);
}

270
static struct attribute *hwmon_genattr(const void *drvdata,
271 272 273 274 275 276 277 278 279 280
				       enum hwmon_sensor_types type,
				       u32 attr,
				       int index,
				       const char *template,
				       const struct hwmon_ops *ops)
{
	struct hwmon_device_attribute *hattr;
	struct device_attribute *dattr;
	struct attribute *a;
	umode_t mode;
281
	const char *name;
282
	bool is_string = is_string_attr(type, attr);
283 284 285 286 287 288 289 290 291

	/* The attribute is invisible if there is no template string */
	if (!template)
		return ERR_PTR(-ENOENT);

	mode = ops->is_visible(drvdata, type, attr, index);
	if (!mode)
		return ERR_PTR(-ENOENT);

292
	if ((mode & 0444) && ((is_string && !ops->read_string) ||
293
				 (!is_string && !ops->read)))
294
		return ERR_PTR(-EINVAL);
295
	if ((mode & 0222) && !ops->write)
296 297
		return ERR_PTR(-EINVAL);

298
	hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
299 300 301
	if (!hattr)
		return ERR_PTR(-ENOMEM);

302
	if (type == hwmon_chip) {
303
		name = template;
304
	} else {
305
		scnprintf(hattr->name, sizeof(hattr->name), template,
306
			  index + hwmon_attr_base(type));
307
		name = hattr->name;
308 309 310 311 312 313 314 315
	}

	hattr->type = type;
	hattr->attr = attr;
	hattr->index = index;
	hattr->ops = ops;

	dattr = &hattr->dev_attr;
316
	dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
317 318 319 320 321 322 323 324 325 326
	dattr->store = hwmon_attr_store;

	a = &dattr->attr;
	sysfs_attr_init(a);
	a->name = name;
	a->mode = mode;

	return a;
}

327 328 329 330 331
/*
 * Chip attributes are not attribute templates but actual sysfs attributes.
 * See hwmon_genattr() for special handling.
 */
static const char * const hwmon_chip_attrs[] = {
332
	[hwmon_chip_temp_reset_history] = "temp_reset_history",
333
	[hwmon_chip_in_reset_history] = "in_reset_history",
334
	[hwmon_chip_curr_reset_history] = "curr_reset_history",
335
	[hwmon_chip_power_reset_history] = "power_reset_history",
336 337
	[hwmon_chip_update_interval] = "update_interval",
	[hwmon_chip_alarms] = "alarms",
338 339 340 341 342
	[hwmon_chip_samples] = "samples",
	[hwmon_chip_curr_samples] = "curr_samples",
	[hwmon_chip_in_samples] = "in_samples",
	[hwmon_chip_power_samples] = "power_samples",
	[hwmon_chip_temp_samples] = "temp_samples",
343 344 345
};

static const char * const hwmon_temp_attr_templates[] = {
346
	[hwmon_temp_enable] = "temp%d_enable",
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
	[hwmon_temp_input] = "temp%d_input",
	[hwmon_temp_type] = "temp%d_type",
	[hwmon_temp_lcrit] = "temp%d_lcrit",
	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
	[hwmon_temp_min] = "temp%d_min",
	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
	[hwmon_temp_max] = "temp%d_max",
	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
	[hwmon_temp_crit] = "temp%d_crit",
	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
	[hwmon_temp_emergency] = "temp%d_emergency",
	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
	[hwmon_temp_alarm] = "temp%d_alarm",
	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
	[hwmon_temp_fault] = "temp%d_fault",
	[hwmon_temp_offset] = "temp%d_offset",
	[hwmon_temp_label] = "temp%d_label",
	[hwmon_temp_lowest] = "temp%d_lowest",
	[hwmon_temp_highest] = "temp%d_highest",
	[hwmon_temp_reset_history] = "temp%d_reset_history",
};

373
static const char * const hwmon_in_attr_templates[] = {
374
	[hwmon_in_enable] = "in%d_enable",
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
	[hwmon_in_input] = "in%d_input",
	[hwmon_in_min] = "in%d_min",
	[hwmon_in_max] = "in%d_max",
	[hwmon_in_lcrit] = "in%d_lcrit",
	[hwmon_in_crit] = "in%d_crit",
	[hwmon_in_average] = "in%d_average",
	[hwmon_in_lowest] = "in%d_lowest",
	[hwmon_in_highest] = "in%d_highest",
	[hwmon_in_reset_history] = "in%d_reset_history",
	[hwmon_in_label] = "in%d_label",
	[hwmon_in_alarm] = "in%d_alarm",
	[hwmon_in_min_alarm] = "in%d_min_alarm",
	[hwmon_in_max_alarm] = "in%d_max_alarm",
	[hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
};

392
static const char * const hwmon_curr_attr_templates[] = {
393
	[hwmon_curr_enable] = "curr%d_enable",
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
	[hwmon_curr_input] = "curr%d_input",
	[hwmon_curr_min] = "curr%d_min",
	[hwmon_curr_max] = "curr%d_max",
	[hwmon_curr_lcrit] = "curr%d_lcrit",
	[hwmon_curr_crit] = "curr%d_crit",
	[hwmon_curr_average] = "curr%d_average",
	[hwmon_curr_lowest] = "curr%d_lowest",
	[hwmon_curr_highest] = "curr%d_highest",
	[hwmon_curr_reset_history] = "curr%d_reset_history",
	[hwmon_curr_label] = "curr%d_label",
	[hwmon_curr_alarm] = "curr%d_alarm",
	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
};

411
static const char * const hwmon_power_attr_templates[] = {
412
	[hwmon_power_enable] = "power%d_enable",
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	[hwmon_power_average] = "power%d_average",
	[hwmon_power_average_interval] = "power%d_average_interval",
	[hwmon_power_average_interval_max] = "power%d_interval_max",
	[hwmon_power_average_interval_min] = "power%d_interval_min",
	[hwmon_power_average_highest] = "power%d_average_highest",
	[hwmon_power_average_lowest] = "power%d_average_lowest",
	[hwmon_power_average_max] = "power%d_average_max",
	[hwmon_power_average_min] = "power%d_average_min",
	[hwmon_power_input] = "power%d_input",
	[hwmon_power_input_highest] = "power%d_input_highest",
	[hwmon_power_input_lowest] = "power%d_input_lowest",
	[hwmon_power_reset_history] = "power%d_reset_history",
	[hwmon_power_accuracy] = "power%d_accuracy",
	[hwmon_power_cap] = "power%d_cap",
	[hwmon_power_cap_hyst] = "power%d_cap_hyst",
	[hwmon_power_cap_max] = "power%d_cap_max",
	[hwmon_power_cap_min] = "power%d_cap_min",
430
	[hwmon_power_min] = "power%d_min",
431
	[hwmon_power_max] = "power%d_max",
432
	[hwmon_power_lcrit] = "power%d_lcrit",
433 434 435 436
	[hwmon_power_crit] = "power%d_crit",
	[hwmon_power_label] = "power%d_label",
	[hwmon_power_alarm] = "power%d_alarm",
	[hwmon_power_cap_alarm] = "power%d_cap_alarm",
437
	[hwmon_power_min_alarm] = "power%d_min_alarm",
438
	[hwmon_power_max_alarm] = "power%d_max_alarm",
439
	[hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
440 441 442
	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
};

443
static const char * const hwmon_energy_attr_templates[] = {
444
	[hwmon_energy_enable] = "energy%d_enable",
445 446 447 448 449
	[hwmon_energy_input] = "energy%d_input",
	[hwmon_energy_label] = "energy%d_label",
};

static const char * const hwmon_humidity_attr_templates[] = {
450
	[hwmon_humidity_enable] = "humidity%d_enable",
451 452 453 454 455 456 457 458 459 460
	[hwmon_humidity_input] = "humidity%d_input",
	[hwmon_humidity_label] = "humidity%d_label",
	[hwmon_humidity_min] = "humidity%d_min",
	[hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
	[hwmon_humidity_max] = "humidity%d_max",
	[hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
	[hwmon_humidity_alarm] = "humidity%d_alarm",
	[hwmon_humidity_fault] = "humidity%d_fault",
};

461
static const char * const hwmon_fan_attr_templates[] = {
462
	[hwmon_fan_enable] = "fan%d_enable",
463 464 465 466 467 468 469 470 471 472 473 474 475
	[hwmon_fan_input] = "fan%d_input",
	[hwmon_fan_label] = "fan%d_label",
	[hwmon_fan_min] = "fan%d_min",
	[hwmon_fan_max] = "fan%d_max",
	[hwmon_fan_div] = "fan%d_div",
	[hwmon_fan_pulses] = "fan%d_pulses",
	[hwmon_fan_target] = "fan%d_target",
	[hwmon_fan_alarm] = "fan%d_alarm",
	[hwmon_fan_min_alarm] = "fan%d_min_alarm",
	[hwmon_fan_max_alarm] = "fan%d_max_alarm",
	[hwmon_fan_fault] = "fan%d_fault",
};

476 477 478 479 480 481 482
static const char * const hwmon_pwm_attr_templates[] = {
	[hwmon_pwm_input] = "pwm%d",
	[hwmon_pwm_enable] = "pwm%d_enable",
	[hwmon_pwm_mode] = "pwm%d_mode",
	[hwmon_pwm_freq] = "pwm%d_freq",
};

483 484 485 486 487
static const char * const hwmon_intrusion_attr_templates[] = {
	[hwmon_intrusion_alarm] = "intrusion%d_alarm",
	[hwmon_intrusion_beep]  = "intrusion%d_beep",
};

488
static const char * const *__templates[] = {
489
	[hwmon_chip] = hwmon_chip_attrs,
490
	[hwmon_temp] = hwmon_temp_attr_templates,
491
	[hwmon_in] = hwmon_in_attr_templates,
492
	[hwmon_curr] = hwmon_curr_attr_templates,
493
	[hwmon_power] = hwmon_power_attr_templates,
494 495
	[hwmon_energy] = hwmon_energy_attr_templates,
	[hwmon_humidity] = hwmon_humidity_attr_templates,
496
	[hwmon_fan] = hwmon_fan_attr_templates,
497
	[hwmon_pwm] = hwmon_pwm_attr_templates,
498
	[hwmon_intrusion] = hwmon_intrusion_attr_templates,
499 500 501
};

static const int __templates_size[] = {
502
	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
503
	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
504
	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
505
	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
506
	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
507 508
	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
509
	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
510
	[hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
511
	[hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
512 513 514 515 516 517 518 519 520 521 522 523
};

static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
{
	int i, n;

	for (i = n = 0; info->config[i]; i++)
		n += hweight32(info->config[i]);

	return n;
}

524
static int hwmon_genattrs(const void *drvdata,
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
			  struct attribute **attrs,
			  const struct hwmon_ops *ops,
			  const struct hwmon_channel_info *info)
{
	const char * const *templates;
	int template_size;
	int i, aindex = 0;

	if (info->type >= ARRAY_SIZE(__templates))
		return -EINVAL;

	templates = __templates[info->type];
	template_size = __templates_size[info->type];

	for (i = 0; info->config[i]; i++) {
		u32 attr_mask = info->config[i];
		u32 attr;

		while (attr_mask) {
			struct attribute *a;

			attr = __ffs(attr_mask);
			attr_mask &= ~BIT(attr);
			if (attr >= template_size)
				return -EINVAL;
550
			a = hwmon_genattr(drvdata, info->type, attr, i,
551 552 553 554 555 556 557 558 559 560 561 562 563
					  templates[attr], ops);
			if (IS_ERR(a)) {
				if (PTR_ERR(a) != -ENOENT)
					return PTR_ERR(a);
				continue;
			}
			attrs[aindex++] = a;
		}
	}
	return aindex;
}

static struct attribute **
564
__hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
565 566 567 568 569 570 571 572 573 574
{
	int ret, i, aindex = 0, nattrs = 0;
	struct attribute **attrs;

	for (i = 0; chip->info[i]; i++)
		nattrs += hwmon_num_channel_attrs(chip->info[i]);

	if (nattrs == 0)
		return ERR_PTR(-EINVAL);

575
	attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
576 577 578 579
	if (!attrs)
		return ERR_PTR(-ENOMEM);

	for (i = 0; chip->info[i]; i++) {
580
		ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
581
				     chip->info[i]);
582 583
		if (ret < 0) {
			hwmon_free_attrs(attrs);
584
			return ERR_PTR(ret);
585
		}
586 587 588 589 590 591 592 593 594 595
		aindex += ret;
	}

	return attrs;
}

static struct device *
__hwmon_device_register(struct device *dev, const char *name, void *drvdata,
			const struct hwmon_chip_info *chip,
			const struct attribute_group **groups)
596
{
597
	struct hwmon_device *hwdev;
598 599
	struct device *hdev;
	int i, j, err, id;
600

601
	/* Complain about invalid characters in hwmon name attribute */
602
	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
603 604 605
		dev_warn(dev,
			 "hwmon: '%s' is not a valid name attribute, please fix\n",
			 name);
606

607 608 609
	id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
	if (id < 0)
		return ERR_PTR(id);
610

611 612 613 614 615
	hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
	if (hwdev == NULL) {
		err = -ENOMEM;
		goto ida_remove;
	}
616

617 618
	hdev = &hwdev->dev;

619
	if (chip) {
620
		struct attribute **attrs;
621
		int ngroups = 2; /* terminating NULL plus &hwdev->groups */
622 623 624 625 626

		if (groups)
			for (i = 0; groups[i]; i++)
				ngroups++;

627
		hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
628 629 630 631
		if (!hwdev->groups) {
			err = -ENOMEM;
			goto free_hwmon;
		}
632

633
		attrs = __hwmon_create_attrs(drvdata, chip);
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
		if (IS_ERR(attrs)) {
			err = PTR_ERR(attrs);
			goto free_hwmon;
		}

		hwdev->group.attrs = attrs;
		ngroups = 0;
		hwdev->groups[ngroups++] = &hwdev->group;

		if (groups) {
			for (i = 0; groups[i]; i++)
				hwdev->groups[ngroups++] = groups[i];
		}

		hdev->groups = hwdev->groups;
	} else {
		hdev->groups = groups;
	}

653
	hwdev->name = name;
654 655 656 657 658 659 660
	hdev->class = &hwmon_class;
	hdev->parent = dev;
	hdev->of_node = dev ? dev->of_node : NULL;
	hwdev->chip = chip;
	dev_set_drvdata(hdev, drvdata);
	dev_set_name(hdev, HWMON_ID_FORMAT, id);
	err = device_register(hdev);
661
	if (err)
662 663
		goto free_hwmon;

664
	if (dev && dev->of_node && chip && chip->ops->read &&
665 666 667 668 669 670 671 672 673 674 675 676
	    chip->info[0]->type == hwmon_chip &&
	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
		const struct hwmon_channel_info **info = chip->info;

		for (i = 1; info[i]; i++) {
			if (info[i]->type != hwmon_temp)
				continue;

			for (j = 0; info[i]->config[j]; j++) {
				if (!chip->ops->is_visible(drvdata, hwmon_temp,
							   hwmon_temp_input, j))
					continue;
677
				if (info[i]->config[j] & HWMON_T_INPUT) {
678
					err = hwmon_thermal_add_sensor(hdev, j);
679 680
					if (err) {
						device_unregister(hdev);
681 682 683 684 685 686
						/*
						 * Don't worry about hwdev;
						 * hwmon_dev_release(), called
						 * from device_unregister(),
						 * will free it.
						 */
687 688
						goto ida_remove;
					}
689
				}
690 691 692
			}
		}
	}
693

694
	return hdev;
695

696
free_hwmon:
697
	hwmon_dev_release(hdev);
698 699 700 701
ida_remove:
	ida_simple_remove(&hwmon_ida, id);
	return ERR_PTR(err);
}
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719

/**
 * hwmon_device_register_with_groups - register w/ hwmon
 * @dev: the parent device
 * @name: hwmon name attribute
 * @drvdata: driver data to attach to created device
 * @groups: List of attribute groups to create
 *
 * hwmon_device_unregister() must be called when the device is no
 * longer needed.
 *
 * Returns the pointer to the new device.
 */
struct device *
hwmon_device_register_with_groups(struct device *dev, const char *name,
				  void *drvdata,
				  const struct attribute_group **groups)
{
720 721 722
	if (!name)
		return ERR_PTR(-EINVAL);

723 724
	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
}
725
EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
726

727 728 729 730 731
/**
 * hwmon_device_register_with_info - register w/ hwmon
 * @dev: the parent device
 * @name: hwmon name attribute
 * @drvdata: driver data to attach to created device
732
 * @chip: pointer to hwmon chip information
733
 * @extra_groups: pointer to list of additional non-standard attribute groups
734 735 736 737 738 739 740 741 742 743
 *
 * hwmon_device_unregister() must be called when the device is no
 * longer needed.
 *
 * Returns the pointer to the new device.
 */
struct device *
hwmon_device_register_with_info(struct device *dev, const char *name,
				void *drvdata,
				const struct hwmon_chip_info *chip,
744
				const struct attribute_group **extra_groups)
745
{
746 747 748
	if (!name)
		return ERR_PTR(-EINVAL);

749
	if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
750 751
		return ERR_PTR(-EINVAL);

752 753 754
	if (chip && !dev)
		return ERR_PTR(-EINVAL);

755
	return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
756 757 758
}
EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);

759 760 761 762 763 764 765 766 767 768 769
/**
 * hwmon_device_register - register w/ hwmon
 * @dev: the device to register
 *
 * hwmon_device_unregister() must be called when the device is no
 * longer needed.
 *
 * Returns the pointer to the new device.
 */
struct device *hwmon_device_register(struct device *dev)
{
770 771 772
	dev_warn(dev,
		 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");

773
	return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
774
}
775
EXPORT_SYMBOL_GPL(hwmon_device_register);
776 777 778 779

/**
 * hwmon_device_unregister - removes the previously registered class device
 *
780
 * @dev: the class device to destroy
781
 */
782
void hwmon_device_unregister(struct device *dev)
783 784 785
{
	int id;

786
	if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
787
		device_unregister(dev);
788
		ida_simple_remove(&hwmon_ida, id);
789
	} else
790
		dev_dbg(dev->parent,
791 792
			"hwmon_device_unregister() failed: bad class ID!\n");
}
793
EXPORT_SYMBOL_GPL(hwmon_device_unregister);
794

795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
static void devm_hwmon_release(struct device *dev, void *res)
{
	struct device *hwdev = *(struct device **)res;

	hwmon_device_unregister(hwdev);
}

/**
 * devm_hwmon_device_register_with_groups - register w/ hwmon
 * @dev: the parent device
 * @name: hwmon name attribute
 * @drvdata: driver data to attach to created device
 * @groups: List of attribute groups to create
 *
 * Returns the pointer to the new device. The new device is automatically
 * unregistered with the parent device.
 */
struct device *
devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
				       void *drvdata,
				       const struct attribute_group **groups)
{
	struct device **ptr, *hwdev;

	if (!dev)
		return ERR_PTR(-EINVAL);

	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
	if (!ptr)
		return ERR_PTR(-ENOMEM);

	hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
	if (IS_ERR(hwdev))
		goto error;

	*ptr = hwdev;
	devres_add(dev, ptr);
	return hwdev;

error:
	devres_free(ptr);
	return hwdev;
}
EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);

840 841
/**
 * devm_hwmon_device_register_with_info - register w/ hwmon
842 843 844 845 846
 * @dev:	the parent device
 * @name:	hwmon name attribute
 * @drvdata:	driver data to attach to created device
 * @chip:	pointer to hwmon chip information
 * @groups:	pointer to list of driver specific attribute groups
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
 *
 * Returns the pointer to the new device. The new device is automatically
 * unregistered with the parent device.
 */
struct device *
devm_hwmon_device_register_with_info(struct device *dev, const char *name,
				     void *drvdata,
				     const struct hwmon_chip_info *chip,
				     const struct attribute_group **groups)
{
	struct device **ptr, *hwdev;

	if (!dev)
		return ERR_PTR(-EINVAL);

	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
	if (!ptr)
		return ERR_PTR(-ENOMEM);

	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
						groups);
	if (IS_ERR(hwdev))
		goto error;

	*ptr = hwdev;
	devres_add(dev, ptr);

	return hwdev;

error:
	devres_free(ptr);
	return hwdev;
}
EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);

882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
static int devm_hwmon_match(struct device *dev, void *res, void *data)
{
	struct device **hwdev = res;

	return *hwdev == data;
}

/**
 * devm_hwmon_device_unregister - removes a previously registered hwmon device
 *
 * @dev: the parent device of the device to unregister
 */
void devm_hwmon_device_unregister(struct device *dev)
{
	WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
}
EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);

900 901 902 903 904 905 906 907 908
static void __init hwmon_pci_quirks(void)
{
#if defined CONFIG_X86 && defined CONFIG_PCI
	struct pci_dev *sb;
	u16 base;
	u8 enable;

	/* Open access to 0x295-0x296 on MSI MS-7031 */
	sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
909 910 911 912 913 914 915 916 917 918 919 920 921
	if (sb) {
		if (sb->subsystem_vendor == 0x1462 &&	/* MSI */
		    sb->subsystem_device == 0x0031) {	/* MS-7031 */
			pci_read_config_byte(sb, 0x48, &enable);
			pci_read_config_word(sb, 0x64, &base);

			if (base == 0 && !(enable & BIT(2))) {
				dev_info(&sb->dev,
					 "Opening wide generic port at 0x295\n");
				pci_write_config_word(sb, 0x64, 0x295);
				pci_write_config_byte(sb, 0x48,
						      enable | BIT(2));
			}
922
		}
923
		pci_dev_put(sb);
924 925 926 927
	}
#endif
}

928 929
static int __init hwmon_init(void)
{
930 931
	int err;

932 933
	hwmon_pci_quirks();

934 935 936 937
	err = class_register(&hwmon_class);
	if (err) {
		pr_err("couldn't register hwmon sysfs class\n");
		return err;
938 939 940 941 942 943
	}
	return 0;
}

static void __exit hwmon_exit(void)
{
944
	class_unregister(&hwmon_class);
945 946
}

D
David Brownell 已提交
947
subsys_initcall(hwmon_init);
948 949 950 951 952 953
module_exit(hwmon_exit);

MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
MODULE_LICENSE("GPL");