xref: /linux/drivers/hwmon/hwmon.c (revision b95ba51883fdfb60ab2633e0a5320a2f26f5298e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
4  *
5  * This file defines the sysfs class "hwmon", for use by sensors drivers.
6  *
7  * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
8  */
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
12 #include <linux/bitops.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/gfp.h>
16 #include <linux/hwmon.h>
17 #include <linux/i2c.h>
18 #include <linux/idr.h>
19 #include <linux/kstrtox.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/pci.h>
24 #include <linux/property.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/thermal.h>
28 
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/hwmon.h>
31 
32 #define HWMON_ID_PREFIX "hwmon"
33 #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
34 
35 struct hwmon_device {
36 	const char *name;
37 	const char *label;
38 	struct device dev;
39 	const struct hwmon_chip_info *chip;
40 	struct mutex lock;
41 	struct list_head tzdata;
42 	struct attribute_group group;
43 	const struct attribute_group **groups;
44 };
45 
46 #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
47 
48 #define MAX_SYSFS_ATTR_NAME_LENGTH	32
49 
50 struct hwmon_device_attribute {
51 	struct device_attribute dev_attr;
52 	const struct hwmon_ops *ops;
53 	enum hwmon_sensor_types type;
54 	u32 attr;
55 	int index;
56 	char name[MAX_SYSFS_ATTR_NAME_LENGTH];
57 };
58 
59 #define to_hwmon_attr(d) \
60 	container_of(d, struct hwmon_device_attribute, dev_attr)
61 #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
62 
63 /*
64  * Thermal zone information
65  */
66 struct hwmon_thermal_data {
67 	struct list_head node;		/* hwmon tzdata list entry */
68 	struct device *dev;		/* Reference to hwmon device */
69 	int index;			/* sensor index */
70 	struct thermal_zone_device *tzd;/* thermal zone device */
71 };
72 
73 static ssize_t
74 name_show(struct device *dev, struct device_attribute *attr, char *buf)
75 {
76 	return sysfs_emit(buf, "%s\n", to_hwmon_device(dev)->name);
77 }
78 static DEVICE_ATTR_RO(name);
79 
80 static ssize_t
81 label_show(struct device *dev, struct device_attribute *attr, char *buf)
82 {
83 	return sysfs_emit(buf, "%s\n", to_hwmon_device(dev)->label);
84 }
85 static DEVICE_ATTR_RO(label);
86 
87 static struct attribute *hwmon_dev_attrs[] = {
88 	&dev_attr_name.attr,
89 	&dev_attr_label.attr,
90 	NULL
91 };
92 
93 static umode_t hwmon_dev_attr_is_visible(struct kobject *kobj,
94 					 struct attribute *attr, int n)
95 {
96 	struct device *dev = kobj_to_dev(kobj);
97 	struct hwmon_device *hdev = to_hwmon_device(dev);
98 
99 	if (attr == &dev_attr_name.attr && hdev->name == NULL)
100 		return 0;
101 
102 	if (attr == &dev_attr_label.attr && hdev->label == NULL)
103 		return 0;
104 
105 	return attr->mode;
106 }
107 
108 static const struct attribute_group hwmon_dev_attr_group = {
109 	.attrs		= hwmon_dev_attrs,
110 	.is_visible	= hwmon_dev_attr_is_visible,
111 };
112 
113 static const struct attribute_group *hwmon_dev_attr_groups[] = {
114 	&hwmon_dev_attr_group,
115 	NULL
116 };
117 
118 static void hwmon_free_attrs(struct attribute **attrs)
119 {
120 	int i;
121 
122 	for (i = 0; attrs[i]; i++) {
123 		struct device_attribute *dattr = to_dev_attr(attrs[i]);
124 		struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr);
125 
126 		kfree(hattr);
127 	}
128 	kfree(attrs);
129 }
130 
131 static void hwmon_dev_release(struct device *dev)
132 {
133 	struct hwmon_device *hwdev = to_hwmon_device(dev);
134 
135 	if (hwdev->group.attrs)
136 		hwmon_free_attrs(hwdev->group.attrs);
137 	kfree(hwdev->groups);
138 	kfree(hwdev->label);
139 	kfree(hwdev);
140 }
141 
142 static const struct class hwmon_class = {
143 	.name = "hwmon",
144 	.dev_groups = hwmon_dev_attr_groups,
145 	.dev_release = hwmon_dev_release,
146 };
147 
148 static DEFINE_IDA(hwmon_ida);
149 
150 static umode_t hwmon_is_visible(const struct hwmon_ops *ops,
151 				const void *drvdata,
152 				enum hwmon_sensor_types type,
153 				u32 attr, int channel)
154 {
155 	if (ops->visible)
156 		return ops->visible;
157 
158 	return ops->is_visible(drvdata, type, attr, channel);
159 }
160 
161 /* Thermal zone handling */
162 
163 static int hwmon_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
164 {
165 	struct hwmon_thermal_data *tdata = thermal_zone_device_priv(tz);
166 	struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
167 	int ret;
168 	long t;
169 
170 	guard(mutex)(&hwdev->lock);
171 
172 	ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
173 				     tdata->index, &t);
174 	if (ret < 0)
175 		return ret;
176 
177 	*temp = t;
178 
179 	return 0;
180 }
181 
182 static int hwmon_thermal_set_trips(struct thermal_zone_device *tz, int low, int high)
183 {
184 	struct hwmon_thermal_data *tdata = thermal_zone_device_priv(tz);
185 	struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
186 	const struct hwmon_chip_info *chip = hwdev->chip;
187 	const struct hwmon_channel_info * const *info = chip->info;
188 	unsigned int i;
189 	int err;
190 
191 	if (!chip->ops->write)
192 		return 0;
193 
194 	for (i = 0; info[i] && info[i]->type != hwmon_temp; i++)
195 		continue;
196 
197 	if (!info[i])
198 		return 0;
199 
200 	guard(mutex)(&hwdev->lock);
201 
202 	if (info[i]->config[tdata->index] & HWMON_T_MIN) {
203 		err = chip->ops->write(tdata->dev, hwmon_temp,
204 				       hwmon_temp_min, tdata->index, low);
205 		if (err && err != -EOPNOTSUPP)
206 			return err;
207 	}
208 
209 	if (info[i]->config[tdata->index] & HWMON_T_MAX) {
210 		err = chip->ops->write(tdata->dev, hwmon_temp,
211 				       hwmon_temp_max, tdata->index, high);
212 		if (err && err != -EOPNOTSUPP)
213 			return err;
214 	}
215 
216 	return 0;
217 }
218 
219 static const struct thermal_zone_device_ops hwmon_thermal_ops = {
220 	.get_temp = hwmon_thermal_get_temp,
221 	.set_trips = hwmon_thermal_set_trips,
222 };
223 
224 static void hwmon_thermal_remove_sensor(void *data)
225 {
226 	list_del(data);
227 }
228 
229 static int hwmon_thermal_add_sensor(struct device *dev, int index)
230 {
231 	struct hwmon_device *hwdev = to_hwmon_device(dev);
232 	struct hwmon_thermal_data *tdata;
233 	struct thermal_zone_device *tzd;
234 	int err;
235 
236 	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
237 	if (!tdata)
238 		return -ENOMEM;
239 
240 	tdata->dev = dev;
241 	tdata->index = index;
242 
243 	tzd = devm_thermal_of_zone_register(dev, index, tdata,
244 					    &hwmon_thermal_ops);
245 	if (IS_ERR(tzd)) {
246 		if (PTR_ERR(tzd) != -ENODEV)
247 			return PTR_ERR(tzd);
248 		dev_info(dev, "temp%d_input not attached to any thermal zone\n",
249 			 index + 1);
250 		devm_kfree(dev, tdata);
251 		return 0;
252 	}
253 
254 	err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node);
255 	if (err)
256 		return err;
257 
258 	tdata->tzd = tzd;
259 	list_add(&tdata->node, &hwdev->tzdata);
260 
261 	return 0;
262 }
263 
264 static int hwmon_thermal_register_sensors(struct device *dev)
265 {
266 	struct hwmon_device *hwdev = to_hwmon_device(dev);
267 	const struct hwmon_chip_info *chip = hwdev->chip;
268 	const struct hwmon_channel_info * const *info = chip->info;
269 	void *drvdata = dev_get_drvdata(dev);
270 	int i;
271 
272 	if (!IS_ENABLED(CONFIG_THERMAL_OF))
273 		return 0;
274 
275 	for (i = 1; info[i]; i++) {
276 		int j;
277 
278 		if (info[i]->type != hwmon_temp)
279 			continue;
280 
281 		for (j = 0; info[i]->config[j]; j++) {
282 			int err;
283 
284 			if (!(info[i]->config[j] & HWMON_T_INPUT) ||
285 			    !hwmon_is_visible(chip->ops, drvdata, hwmon_temp,
286 					      hwmon_temp_input, j))
287 				continue;
288 
289 			err = hwmon_thermal_add_sensor(dev, j);
290 			if (err)
291 				return err;
292 		}
293 	}
294 
295 	return 0;
296 }
297 
298 static void hwmon_thermal_notify(struct device *dev, int index)
299 {
300 	struct hwmon_device *hwdev = to_hwmon_device(dev);
301 	struct hwmon_thermal_data *tzdata;
302 
303 	if (!IS_ENABLED(CONFIG_THERMAL_OF))
304 		return;
305 
306 	list_for_each_entry(tzdata, &hwdev->tzdata, node) {
307 		if (tzdata->index == index) {
308 			thermal_zone_device_update(tzdata->tzd,
309 						   THERMAL_EVENT_UNSPECIFIED);
310 		}
311 	}
312 }
313 
314 static int hwmon_attr_base(enum hwmon_sensor_types type)
315 {
316 	if (type == hwmon_in || type == hwmon_intrusion)
317 		return 0;
318 	return 1;
319 }
320 
321 #if IS_REACHABLE(CONFIG_I2C)
322 
323 /*
324  * PEC support
325  *
326  * The 'pec' attribute is attached to I2C client devices. It is only provided
327  * if the i2c controller supports PEC.
328  *
329  * The mutex ensures that PEC configuration between i2c device and the hardware
330  * is consistent. Use a single mutex because attribute writes are supposed to be
331  * rare, and maintaining a separate mutex for each hardware monitoring device
332  * would add substantial complexity to the driver for little if any gain.
333  *
334  * The hardware monitoring device is identified as child of the i2c client
335  * device. This assumes that only a single hardware monitoring device is
336  * attached to an i2c client device.
337  */
338 
339 static int hwmon_match_device(struct device *dev, const void *data)
340 {
341 	return dev->class == &hwmon_class;
342 }
343 
344 static ssize_t pec_show(struct device *dev, struct device_attribute *dummy,
345 			char *buf)
346 {
347 	struct i2c_client *client = to_i2c_client(dev);
348 
349 	return sysfs_emit(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
350 }
351 
352 static ssize_t pec_store(struct device *dev, struct device_attribute *devattr,
353 			 const char *buf, size_t count)
354 {
355 	struct i2c_client *client = to_i2c_client(dev);
356 	struct hwmon_device *hwdev;
357 	struct device *hdev;
358 	bool val;
359 	int err;
360 
361 	err = kstrtobool(buf, &val);
362 	if (err < 0)
363 		return err;
364 
365 	hdev = device_find_child(dev, NULL, hwmon_match_device);
366 	if (!hdev)
367 		return -ENODEV;
368 
369 	/*
370 	 * If there is no write function, we assume that chip specific
371 	 * handling is not required.
372 	 */
373 	hwdev = to_hwmon_device(hdev);
374 	guard(mutex)(&hwdev->lock);
375 	if (hwdev->chip->ops->write) {
376 		err = hwdev->chip->ops->write(hdev, hwmon_chip, hwmon_chip_pec, 0, val);
377 		if (err && err != -EOPNOTSUPP)
378 			goto put;
379 	}
380 
381 	if (!val)
382 		client->flags &= ~I2C_CLIENT_PEC;
383 	else
384 		client->flags |= I2C_CLIENT_PEC;
385 
386 	err = count;
387 put:
388 	put_device(hdev);
389 
390 	return err;
391 }
392 
393 static DEVICE_ATTR_RW(pec);
394 
395 static void hwmon_remove_pec(void *dev)
396 {
397 	device_remove_file(dev, &dev_attr_pec);
398 }
399 
400 static int hwmon_pec_register(struct device *hdev)
401 {
402 	struct i2c_client *client = i2c_verify_client(hdev->parent);
403 	int err;
404 
405 	if (!client)
406 		return -EINVAL;
407 
408 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_PEC))
409 		return 0;
410 
411 	err = device_create_file(&client->dev, &dev_attr_pec);
412 	if (err)
413 		return err;
414 
415 	return devm_add_action_or_reset(hdev, hwmon_remove_pec, &client->dev);
416 }
417 
418 #else /* CONFIG_I2C */
419 static int hwmon_pec_register(struct device *hdev)
420 {
421 	return -EINVAL;
422 }
423 #endif /* CONFIG_I2C */
424 
425 /* sysfs attribute management */
426 
427 static ssize_t hwmon_attr_show(struct device *dev,
428 			       struct device_attribute *devattr, char *buf)
429 {
430 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
431 	struct hwmon_device *hwdev = to_hwmon_device(dev);
432 	s64 val64;
433 	long val;
434 	int ret;
435 
436 	guard(mutex)(&hwdev->lock);
437 
438 	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
439 			       (hattr->type == hwmon_energy64) ? (long *)&val64 : &val);
440 	if (ret < 0)
441 		return ret;
442 
443 	if (hattr->type != hwmon_energy64)
444 		val64 = val;
445 
446 	trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
447 			      hattr->name, val64);
448 
449 	return sysfs_emit(buf, "%lld\n", val64);
450 }
451 
452 static ssize_t hwmon_attr_show_string(struct device *dev,
453 				      struct device_attribute *devattr,
454 				      char *buf)
455 {
456 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
457 	struct hwmon_device *hwdev = to_hwmon_device(dev);
458 	enum hwmon_sensor_types type = hattr->type;
459 	const char *s;
460 	int ret;
461 
462 	guard(mutex)(&hwdev->lock);
463 
464 	ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
465 				      hattr->index, &s);
466 	if (ret < 0)
467 		return ret;
468 
469 	trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
470 				     hattr->name, s);
471 
472 	return sysfs_emit(buf, "%s\n", s);
473 }
474 
475 static ssize_t hwmon_attr_store(struct device *dev,
476 				struct device_attribute *devattr,
477 				const char *buf, size_t count)
478 {
479 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
480 	struct hwmon_device *hwdev = to_hwmon_device(dev);
481 	long val;
482 	int ret;
483 
484 	ret = kstrtol(buf, 10, &val);
485 	if (ret < 0)
486 		return ret;
487 
488 	guard(mutex)(&hwdev->lock);
489 
490 	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
491 				val);
492 	if (ret < 0)
493 		return ret;
494 
495 	trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
496 			       hattr->name, (s64)val);
497 
498 	return count;
499 }
500 
501 static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
502 {
503 	return (type == hwmon_temp && attr == hwmon_temp_label) ||
504 	       (type == hwmon_in && attr == hwmon_in_label) ||
505 	       (type == hwmon_curr && attr == hwmon_curr_label) ||
506 	       (type == hwmon_power && attr == hwmon_power_label) ||
507 	       (type == hwmon_energy && attr == hwmon_energy_label) ||
508 	       (type == hwmon_energy64 && attr == hwmon_energy_label) ||
509 	       (type == hwmon_humidity && attr == hwmon_humidity_label) ||
510 	       (type == hwmon_fan && attr == hwmon_fan_label);
511 }
512 
513 static struct attribute *hwmon_genattr(const void *drvdata,
514 				       enum hwmon_sensor_types type,
515 				       u32 attr,
516 				       int index,
517 				       const char *template,
518 				       const struct hwmon_ops *ops)
519 {
520 	struct hwmon_device_attribute *hattr;
521 	struct device_attribute *dattr;
522 	struct attribute *a;
523 	umode_t mode;
524 	const char *name;
525 	bool is_string = is_string_attr(type, attr);
526 
527 	mode = hwmon_is_visible(ops, drvdata, type, attr, index);
528 	if (!mode)
529 		return ERR_PTR(-ENOENT);
530 
531 	if ((mode & 0444) && ((is_string && !ops->read_string) ||
532 				 (!is_string && !ops->read)))
533 		return ERR_PTR(-EINVAL);
534 	if ((mode & 0222) && !ops->write)
535 		return ERR_PTR(-EINVAL);
536 
537 	hattr = kzalloc_obj(*hattr);
538 	if (!hattr)
539 		return ERR_PTR(-ENOMEM);
540 
541 	if (type == hwmon_chip) {
542 		name = template;
543 	} else {
544 		scnprintf(hattr->name, sizeof(hattr->name), template,
545 			  index + hwmon_attr_base(type));
546 		name = hattr->name;
547 	}
548 
549 	hattr->type = type;
550 	hattr->attr = attr;
551 	hattr->index = index;
552 	hattr->ops = ops;
553 
554 	dattr = &hattr->dev_attr;
555 	dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
556 	dattr->store = hwmon_attr_store;
557 
558 	a = &dattr->attr;
559 	sysfs_attr_init(a);
560 	a->name = name;
561 	a->mode = mode;
562 
563 	return a;
564 }
565 
566 /*
567  * Chip attributes are not attribute templates but actual sysfs attributes.
568  * See hwmon_genattr() for special handling.
569  */
570 static const char * const hwmon_chip_attrs[] = {
571 	[hwmon_chip_temp_reset_history] = "temp_reset_history",
572 	[hwmon_chip_in_reset_history] = "in_reset_history",
573 	[hwmon_chip_curr_reset_history] = "curr_reset_history",
574 	[hwmon_chip_power_reset_history] = "power_reset_history",
575 	[hwmon_chip_update_interval] = "update_interval",
576 	[hwmon_chip_alarms] = "alarms",
577 	[hwmon_chip_samples] = "samples",
578 	[hwmon_chip_curr_samples] = "curr_samples",
579 	[hwmon_chip_in_samples] = "in_samples",
580 	[hwmon_chip_power_samples] = "power_samples",
581 	[hwmon_chip_temp_samples] = "temp_samples",
582 	[hwmon_chip_beep_enable] = "beep_enable",
583 };
584 
585 static const char * const hwmon_temp_attr_templates[] = {
586 	[hwmon_temp_enable] = "temp%d_enable",
587 	[hwmon_temp_input] = "temp%d_input",
588 	[hwmon_temp_type] = "temp%d_type",
589 	[hwmon_temp_lcrit] = "temp%d_lcrit",
590 	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
591 	[hwmon_temp_min] = "temp%d_min",
592 	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
593 	[hwmon_temp_max] = "temp%d_max",
594 	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
595 	[hwmon_temp_crit] = "temp%d_crit",
596 	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
597 	[hwmon_temp_emergency] = "temp%d_emergency",
598 	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
599 	[hwmon_temp_alarm] = "temp%d_alarm",
600 	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
601 	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
602 	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
603 	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
604 	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
605 	[hwmon_temp_fault] = "temp%d_fault",
606 	[hwmon_temp_offset] = "temp%d_offset",
607 	[hwmon_temp_label] = "temp%d_label",
608 	[hwmon_temp_lowest] = "temp%d_lowest",
609 	[hwmon_temp_highest] = "temp%d_highest",
610 	[hwmon_temp_reset_history] = "temp%d_reset_history",
611 	[hwmon_temp_rated_min] = "temp%d_rated_min",
612 	[hwmon_temp_rated_max] = "temp%d_rated_max",
613 	[hwmon_temp_beep] = "temp%d_beep",
614 };
615 
616 static const char * const hwmon_in_attr_templates[] = {
617 	[hwmon_in_enable] = "in%d_enable",
618 	[hwmon_in_input] = "in%d_input",
619 	[hwmon_in_min] = "in%d_min",
620 	[hwmon_in_max] = "in%d_max",
621 	[hwmon_in_lcrit] = "in%d_lcrit",
622 	[hwmon_in_crit] = "in%d_crit",
623 	[hwmon_in_average] = "in%d_average",
624 	[hwmon_in_lowest] = "in%d_lowest",
625 	[hwmon_in_highest] = "in%d_highest",
626 	[hwmon_in_reset_history] = "in%d_reset_history",
627 	[hwmon_in_label] = "in%d_label",
628 	[hwmon_in_alarm] = "in%d_alarm",
629 	[hwmon_in_min_alarm] = "in%d_min_alarm",
630 	[hwmon_in_max_alarm] = "in%d_max_alarm",
631 	[hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
632 	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
633 	[hwmon_in_rated_min] = "in%d_rated_min",
634 	[hwmon_in_rated_max] = "in%d_rated_max",
635 	[hwmon_in_beep] = "in%d_beep",
636 	[hwmon_in_fault] = "in%d_fault",
637 };
638 
639 static const char * const hwmon_curr_attr_templates[] = {
640 	[hwmon_curr_enable] = "curr%d_enable",
641 	[hwmon_curr_input] = "curr%d_input",
642 	[hwmon_curr_min] = "curr%d_min",
643 	[hwmon_curr_max] = "curr%d_max",
644 	[hwmon_curr_lcrit] = "curr%d_lcrit",
645 	[hwmon_curr_crit] = "curr%d_crit",
646 	[hwmon_curr_average] = "curr%d_average",
647 	[hwmon_curr_lowest] = "curr%d_lowest",
648 	[hwmon_curr_highest] = "curr%d_highest",
649 	[hwmon_curr_reset_history] = "curr%d_reset_history",
650 	[hwmon_curr_label] = "curr%d_label",
651 	[hwmon_curr_alarm] = "curr%d_alarm",
652 	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
653 	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
654 	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
655 	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
656 	[hwmon_curr_rated_min] = "curr%d_rated_min",
657 	[hwmon_curr_rated_max] = "curr%d_rated_max",
658 	[hwmon_curr_beep] = "curr%d_beep",
659 };
660 
661 static const char * const hwmon_power_attr_templates[] = {
662 	[hwmon_power_enable] = "power%d_enable",
663 	[hwmon_power_average] = "power%d_average",
664 	[hwmon_power_average_interval] = "power%d_average_interval",
665 	[hwmon_power_average_interval_max] = "power%d_average_interval_max",
666 	[hwmon_power_average_interval_min] = "power%d_average_interval_min",
667 	[hwmon_power_average_highest] = "power%d_average_highest",
668 	[hwmon_power_average_lowest] = "power%d_average_lowest",
669 	[hwmon_power_average_max] = "power%d_average_max",
670 	[hwmon_power_average_min] = "power%d_average_min",
671 	[hwmon_power_input] = "power%d_input",
672 	[hwmon_power_input_highest] = "power%d_input_highest",
673 	[hwmon_power_input_lowest] = "power%d_input_lowest",
674 	[hwmon_power_reset_history] = "power%d_reset_history",
675 	[hwmon_power_accuracy] = "power%d_accuracy",
676 	[hwmon_power_cap] = "power%d_cap",
677 	[hwmon_power_cap_hyst] = "power%d_cap_hyst",
678 	[hwmon_power_cap_max] = "power%d_cap_max",
679 	[hwmon_power_cap_min] = "power%d_cap_min",
680 	[hwmon_power_min] = "power%d_min",
681 	[hwmon_power_max] = "power%d_max",
682 	[hwmon_power_lcrit] = "power%d_lcrit",
683 	[hwmon_power_crit] = "power%d_crit",
684 	[hwmon_power_label] = "power%d_label",
685 	[hwmon_power_alarm] = "power%d_alarm",
686 	[hwmon_power_cap_alarm] = "power%d_cap_alarm",
687 	[hwmon_power_min_alarm] = "power%d_min_alarm",
688 	[hwmon_power_max_alarm] = "power%d_max_alarm",
689 	[hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
690 	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
691 	[hwmon_power_rated_min] = "power%d_rated_min",
692 	[hwmon_power_rated_max] = "power%d_rated_max",
693 };
694 
695 static const char * const hwmon_energy_attr_templates[] = {
696 	[hwmon_energy_enable] = "energy%d_enable",
697 	[hwmon_energy_input] = "energy%d_input",
698 	[hwmon_energy_label] = "energy%d_label",
699 };
700 
701 static const char * const hwmon_humidity_attr_templates[] = {
702 	[hwmon_humidity_enable] = "humidity%d_enable",
703 	[hwmon_humidity_input] = "humidity%d_input",
704 	[hwmon_humidity_label] = "humidity%d_label",
705 	[hwmon_humidity_min] = "humidity%d_min",
706 	[hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
707 	[hwmon_humidity_max] = "humidity%d_max",
708 	[hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
709 	[hwmon_humidity_alarm] = "humidity%d_alarm",
710 	[hwmon_humidity_fault] = "humidity%d_fault",
711 	[hwmon_humidity_rated_min] = "humidity%d_rated_min",
712 	[hwmon_humidity_rated_max] = "humidity%d_rated_max",
713 	[hwmon_humidity_min_alarm] = "humidity%d_min_alarm",
714 	[hwmon_humidity_max_alarm] = "humidity%d_max_alarm",
715 };
716 
717 static const char * const hwmon_fan_attr_templates[] = {
718 	[hwmon_fan_enable] = "fan%d_enable",
719 	[hwmon_fan_input] = "fan%d_input",
720 	[hwmon_fan_label] = "fan%d_label",
721 	[hwmon_fan_min] = "fan%d_min",
722 	[hwmon_fan_max] = "fan%d_max",
723 	[hwmon_fan_div] = "fan%d_div",
724 	[hwmon_fan_pulses] = "fan%d_pulses",
725 	[hwmon_fan_target] = "fan%d_target",
726 	[hwmon_fan_alarm] = "fan%d_alarm",
727 	[hwmon_fan_min_alarm] = "fan%d_min_alarm",
728 	[hwmon_fan_max_alarm] = "fan%d_max_alarm",
729 	[hwmon_fan_fault] = "fan%d_fault",
730 	[hwmon_fan_beep] = "fan%d_beep",
731 };
732 
733 static const char * const hwmon_pwm_attr_templates[] = {
734 	[hwmon_pwm_input] = "pwm%d",
735 	[hwmon_pwm_enable] = "pwm%d_enable",
736 	[hwmon_pwm_mode] = "pwm%d_mode",
737 	[hwmon_pwm_freq] = "pwm%d_freq",
738 	[hwmon_pwm_auto_channels_temp] = "pwm%d_auto_channels_temp",
739 };
740 
741 static const char * const hwmon_intrusion_attr_templates[] = {
742 	[hwmon_intrusion_alarm] = "intrusion%d_alarm",
743 	[hwmon_intrusion_beep]  = "intrusion%d_beep",
744 };
745 
746 static const char * const *__templates[] = {
747 	[hwmon_chip] = hwmon_chip_attrs,
748 	[hwmon_temp] = hwmon_temp_attr_templates,
749 	[hwmon_in] = hwmon_in_attr_templates,
750 	[hwmon_curr] = hwmon_curr_attr_templates,
751 	[hwmon_power] = hwmon_power_attr_templates,
752 	[hwmon_energy] = hwmon_energy_attr_templates,
753 	[hwmon_energy64] = hwmon_energy_attr_templates,
754 	[hwmon_humidity] = hwmon_humidity_attr_templates,
755 	[hwmon_fan] = hwmon_fan_attr_templates,
756 	[hwmon_pwm] = hwmon_pwm_attr_templates,
757 	[hwmon_intrusion] = hwmon_intrusion_attr_templates,
758 };
759 
760 static const int __templates_size[] = {
761 	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
762 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
763 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
764 	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
765 	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
766 	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
767 	[hwmon_energy64] = ARRAY_SIZE(hwmon_energy_attr_templates),
768 	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
769 	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
770 	[hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
771 	[hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
772 };
773 
774 int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
775 		       u32 attr, int channel)
776 {
777 	char event[MAX_SYSFS_ATTR_NAME_LENGTH + 5];
778 	char sattr[MAX_SYSFS_ATTR_NAME_LENGTH];
779 	char *envp[] = { event, NULL };
780 	const char * const *templates;
781 	const char *template;
782 	int base;
783 
784 	if (type >= ARRAY_SIZE(__templates))
785 		return -EINVAL;
786 	if (attr >= __templates_size[type])
787 		return -EINVAL;
788 
789 	templates = __templates[type];
790 	template = templates[attr];
791 
792 	base = hwmon_attr_base(type);
793 
794 	scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel);
795 	scnprintf(event, sizeof(event), "NAME=%s", sattr);
796 	sysfs_notify(&dev->kobj, NULL, sattr);
797 	kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
798 
799 	if (type == hwmon_temp)
800 		hwmon_thermal_notify(dev, channel);
801 
802 	return 0;
803 }
804 EXPORT_SYMBOL_GPL(hwmon_notify_event);
805 
806 void hwmon_lock(struct device *dev)
807 {
808 	struct hwmon_device *hwdev = to_hwmon_device(dev);
809 
810 	mutex_lock(&hwdev->lock);
811 }
812 EXPORT_SYMBOL_GPL(hwmon_lock);
813 
814 void hwmon_unlock(struct device *dev)
815 {
816 	struct hwmon_device *hwdev = to_hwmon_device(dev);
817 
818 	mutex_unlock(&hwdev->lock);
819 }
820 EXPORT_SYMBOL_GPL(hwmon_unlock);
821 
822 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
823 {
824 	int i, n;
825 
826 	for (i = n = 0; info->config[i]; i++)
827 		n += hweight32(info->config[i]);
828 
829 	return n;
830 }
831 
832 static int hwmon_genattrs(const void *drvdata,
833 			  struct attribute **attrs,
834 			  const struct hwmon_ops *ops,
835 			  const struct hwmon_channel_info *info)
836 {
837 	const char * const *templates;
838 	int template_size;
839 	int i, aindex = 0;
840 
841 	if (info->type >= ARRAY_SIZE(__templates))
842 		return -EINVAL;
843 
844 	templates = __templates[info->type];
845 	template_size = __templates_size[info->type];
846 
847 	for (i = 0; info->config[i]; i++) {
848 		u32 attr_mask = info->config[i];
849 		u32 attr;
850 
851 		while (attr_mask) {
852 			struct attribute *a;
853 
854 			attr = __ffs(attr_mask);
855 			attr_mask &= ~BIT(attr);
856 			if (attr >= template_size || !templates[attr])
857 				continue;	/* attribute is invisible */
858 			a = hwmon_genattr(drvdata, info->type, attr, i,
859 					  templates[attr], ops);
860 			if (IS_ERR(a)) {
861 				if (PTR_ERR(a) != -ENOENT)
862 					return PTR_ERR(a);
863 				continue;
864 			}
865 			attrs[aindex++] = a;
866 		}
867 	}
868 	return aindex;
869 }
870 
871 static struct attribute **
872 __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
873 {
874 	int ret, i, aindex = 0, nattrs = 0;
875 	struct attribute **attrs;
876 
877 	for (i = 0; chip->info[i]; i++)
878 		nattrs += hwmon_num_channel_attrs(chip->info[i]);
879 
880 	if (nattrs == 0)
881 		return ERR_PTR(-EINVAL);
882 
883 	attrs = kzalloc_objs(*attrs, nattrs + 1);
884 	if (!attrs)
885 		return ERR_PTR(-ENOMEM);
886 
887 	for (i = 0; chip->info[i]; i++) {
888 		ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
889 				     chip->info[i]);
890 		if (ret < 0) {
891 			hwmon_free_attrs(attrs);
892 			return ERR_PTR(ret);
893 		}
894 		aindex += ret;
895 	}
896 
897 	return attrs;
898 }
899 
900 static struct device *
901 __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
902 			const struct hwmon_chip_info *chip,
903 			const struct attribute_group **groups)
904 {
905 	struct hwmon_device *hwdev;
906 	const char *label;
907 	struct device *hdev;
908 	struct device *tdev = dev;
909 	int i, err, id;
910 
911 	/* Complain about invalid characters in hwmon name attribute */
912 	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
913 		dev_warn(dev,
914 			 "hwmon: '%s' is not a valid name attribute, please fix\n",
915 			 name);
916 
917 	id = ida_alloc(&hwmon_ida, GFP_KERNEL);
918 	if (id < 0)
919 		return ERR_PTR(id);
920 
921 	hwdev = kzalloc_obj(*hwdev);
922 	if (hwdev == NULL) {
923 		err = -ENOMEM;
924 		goto ida_remove;
925 	}
926 
927 	hdev = &hwdev->dev;
928 
929 	if (chip) {
930 		struct attribute **attrs;
931 		int ngroups = 2; /* terminating NULL plus &hwdev->groups */
932 
933 		if (groups)
934 			for (i = 0; groups[i]; i++)
935 				ngroups++;
936 
937 		hwdev->groups = kzalloc_objs(*groups, ngroups);
938 		if (!hwdev->groups) {
939 			err = -ENOMEM;
940 			goto free_hwmon;
941 		}
942 
943 		attrs = __hwmon_create_attrs(drvdata, chip);
944 		if (IS_ERR(attrs)) {
945 			err = PTR_ERR(attrs);
946 			goto free_hwmon;
947 		}
948 
949 		hwdev->group.attrs = attrs;
950 		ngroups = 0;
951 		hwdev->groups[ngroups++] = &hwdev->group;
952 
953 		if (groups) {
954 			for (i = 0; groups[i]; i++)
955 				hwdev->groups[ngroups++] = groups[i];
956 		}
957 
958 		hdev->groups = hwdev->groups;
959 	} else {
960 		hdev->groups = groups;
961 	}
962 
963 	if (dev && device_property_present(dev, "label")) {
964 		err = device_property_read_string(dev, "label", &label);
965 		if (err < 0)
966 			goto free_hwmon;
967 
968 		hwdev->label = kstrdup(label, GFP_KERNEL);
969 		if (hwdev->label == NULL) {
970 			err = -ENOMEM;
971 			goto free_hwmon;
972 		}
973 	}
974 
975 	hwdev->name = name;
976 	hdev->class = &hwmon_class;
977 	hdev->parent = dev;
978 	while (tdev && !tdev->of_node)
979 		tdev = tdev->parent;
980 	hdev->of_node = tdev ? tdev->of_node : NULL;
981 	hwdev->chip = chip;
982 	mutex_init(&hwdev->lock);
983 	dev_set_drvdata(hdev, drvdata);
984 	dev_set_name(hdev, HWMON_ID_FORMAT, id);
985 	err = device_register(hdev);
986 	if (err) {
987 		put_device(hdev);
988 		goto ida_remove;
989 	}
990 
991 	INIT_LIST_HEAD(&hwdev->tzdata);
992 
993 	if (hdev->of_node && chip && chip->ops->read &&
994 	    chip->info[0]->type == hwmon_chip) {
995 		u32 config = chip->info[0]->config[0];
996 
997 		if (config & HWMON_C_REGISTER_TZ) {
998 			err = hwmon_thermal_register_sensors(hdev);
999 			if (err) {
1000 				device_unregister(hdev);
1001 				/*
1002 				 * Don't worry about hwdev; hwmon_dev_release(),
1003 				 * called from device_unregister(), will free it.
1004 				 */
1005 				goto ida_remove;
1006 			}
1007 		}
1008 		if (config & HWMON_C_PEC) {
1009 			err = hwmon_pec_register(hdev);
1010 			if (err) {
1011 				device_unregister(hdev);
1012 				goto ida_remove;
1013 			}
1014 		}
1015 	}
1016 
1017 	return hdev;
1018 
1019 free_hwmon:
1020 	hwmon_dev_release(hdev);
1021 ida_remove:
1022 	ida_free(&hwmon_ida, id);
1023 	return ERR_PTR(err);
1024 }
1025 
1026 /**
1027  * hwmon_device_register_with_groups - register w/ hwmon
1028  * @dev: the parent device
1029  * @name: hwmon name attribute
1030  * @drvdata: driver data to attach to created device
1031  * @groups: List of attribute groups to create
1032  *
1033  * hwmon_device_unregister() must be called when the device is no
1034  * longer needed.
1035  *
1036  * Returns the pointer to the new device.
1037  */
1038 struct device *
1039 hwmon_device_register_with_groups(struct device *dev, const char *name,
1040 				  void *drvdata,
1041 				  const struct attribute_group **groups)
1042 {
1043 	if (!name)
1044 		return ERR_PTR(-EINVAL);
1045 
1046 	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
1047 }
1048 EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
1049 
1050 /**
1051  * hwmon_device_register_with_info - register w/ hwmon
1052  * @dev: the parent device (mandatory)
1053  * @name: hwmon name attribute (mandatory)
1054  * @drvdata: driver data to attach to created device (optional)
1055  * @chip: pointer to hwmon chip information (mandatory)
1056  * @extra_groups: pointer to list of additional non-standard attribute groups
1057  *	(optional)
1058  *
1059  * hwmon_device_unregister() must be called when the device is no
1060  * longer needed.
1061  *
1062  * Returns the pointer to the new device.
1063  */
1064 struct device *
1065 hwmon_device_register_with_info(struct device *dev, const char *name,
1066 				void *drvdata,
1067 				const struct hwmon_chip_info *chip,
1068 				const struct attribute_group **extra_groups)
1069 {
1070 	if (!dev || !name || !chip)
1071 		return ERR_PTR(-EINVAL);
1072 
1073 	if (!chip->ops || !(chip->ops->visible || chip->ops->is_visible) || !chip->info)
1074 		return ERR_PTR(-EINVAL);
1075 
1076 	return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
1077 }
1078 EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
1079 
1080 /**
1081  * hwmon_device_register_for_thermal - register hwmon device for thermal subsystem
1082  * @dev: the parent device
1083  * @name: hwmon name attribute
1084  * @drvdata: driver data to attach to created device
1085  *
1086  * The use of this function is restricted. It is provided for legacy reasons
1087  * and must only be called from the thermal subsystem.
1088  *
1089  * hwmon_device_unregister() must be called when the device is no
1090  * longer needed.
1091  *
1092  * Returns the pointer to the new device.
1093  */
1094 struct device *
1095 hwmon_device_register_for_thermal(struct device *dev, const char *name,
1096 				  void *drvdata)
1097 {
1098 	if (!name || !dev)
1099 		return ERR_PTR(-EINVAL);
1100 
1101 	return __hwmon_device_register(dev, name, drvdata, NULL, NULL);
1102 }
1103 EXPORT_SYMBOL_NS_GPL(hwmon_device_register_for_thermal, "HWMON_THERMAL");
1104 
1105 /**
1106  * hwmon_device_register - register w/ hwmon
1107  * @dev: the device to register
1108  *
1109  * hwmon_device_unregister() must be called when the device is no
1110  * longer needed.
1111  *
1112  * Returns the pointer to the new device.
1113  */
1114 struct device *hwmon_device_register(struct device *dev)
1115 {
1116 	dev_warn(dev,
1117 		 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
1118 
1119 	return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
1120 }
1121 EXPORT_SYMBOL_GPL(hwmon_device_register);
1122 
1123 /**
1124  * hwmon_device_unregister - removes the previously registered class device
1125  *
1126  * @dev: the class device to destroy
1127  */
1128 void hwmon_device_unregister(struct device *dev)
1129 {
1130 	int id;
1131 
1132 	if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
1133 		device_unregister(dev);
1134 		ida_free(&hwmon_ida, id);
1135 	} else
1136 		dev_dbg(dev->parent,
1137 			"hwmon_device_unregister() failed: bad class ID!\n");
1138 }
1139 EXPORT_SYMBOL_GPL(hwmon_device_unregister);
1140 
1141 static void devm_hwmon_release(struct device *dev, void *res)
1142 {
1143 	struct device *hwdev = *(struct device **)res;
1144 
1145 	hwmon_device_unregister(hwdev);
1146 }
1147 
1148 /**
1149  * devm_hwmon_device_register_with_groups - register w/ hwmon
1150  * @dev: the parent device
1151  * @name: hwmon name attribute
1152  * @drvdata: driver data to attach to created device
1153  * @groups: List of attribute groups to create
1154  *
1155  * Returns the pointer to the new device. The new device is automatically
1156  * unregistered with the parent device.
1157  */
1158 struct device *
1159 devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
1160 				       void *drvdata,
1161 				       const struct attribute_group **groups)
1162 {
1163 	struct device **ptr, *hwdev;
1164 
1165 	if (!dev)
1166 		return ERR_PTR(-EINVAL);
1167 
1168 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
1169 	if (!ptr)
1170 		return ERR_PTR(-ENOMEM);
1171 
1172 	hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
1173 	if (IS_ERR(hwdev))
1174 		goto error;
1175 
1176 	*ptr = hwdev;
1177 	devres_add(dev, ptr);
1178 	return hwdev;
1179 
1180 error:
1181 	devres_free(ptr);
1182 	return hwdev;
1183 }
1184 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
1185 
1186 /**
1187  * devm_hwmon_device_register_with_info - register w/ hwmon
1188  * @dev:	the parent device
1189  * @name:	hwmon name attribute
1190  * @drvdata:	driver data to attach to created device
1191  * @chip:	pointer to hwmon chip information
1192  * @extra_groups: pointer to list of driver specific attribute groups
1193  *
1194  * Returns the pointer to the new device. The new device is automatically
1195  * unregistered with the parent device.
1196  */
1197 struct device *
1198 devm_hwmon_device_register_with_info(struct device *dev, const char *name,
1199 				     void *drvdata,
1200 				     const struct hwmon_chip_info *chip,
1201 				     const struct attribute_group **extra_groups)
1202 {
1203 	struct device **ptr, *hwdev;
1204 
1205 	if (!dev)
1206 		return ERR_PTR(-EINVAL);
1207 
1208 	if (!name) {
1209 		name = devm_hwmon_sanitize_name(dev, dev_name(dev));
1210 		if (IS_ERR(name))
1211 			return ERR_CAST(name);
1212 	}
1213 
1214 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
1215 	if (!ptr)
1216 		return ERR_PTR(-ENOMEM);
1217 
1218 	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
1219 						extra_groups);
1220 	if (IS_ERR(hwdev))
1221 		goto error;
1222 
1223 	*ptr = hwdev;
1224 	devres_add(dev, ptr);
1225 
1226 	return hwdev;
1227 
1228 error:
1229 	devres_free(ptr);
1230 	return hwdev;
1231 }
1232 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
1233 
1234 static char *__hwmon_sanitize_name(struct device *dev, const char *old_name)
1235 {
1236 	char *name, *p;
1237 
1238 	if (dev)
1239 		name = devm_kstrdup(dev, old_name, GFP_KERNEL);
1240 	else
1241 		name = kstrdup(old_name, GFP_KERNEL);
1242 	if (!name)
1243 		return ERR_PTR(-ENOMEM);
1244 
1245 	for (p = name; *p; p++)
1246 		if (hwmon_is_bad_char(*p))
1247 			*p = '_';
1248 
1249 	return name;
1250 }
1251 
1252 /**
1253  * hwmon_sanitize_name - Replaces invalid characters in a hwmon name
1254  * @name: NUL-terminated name
1255  *
1256  * Allocates a new string where any invalid characters will be replaced
1257  * by an underscore. It is the responsibility of the caller to release
1258  * the memory.
1259  *
1260  * Returns newly allocated name, or ERR_PTR on error.
1261  */
1262 char *hwmon_sanitize_name(const char *name)
1263 {
1264 	if (!name)
1265 		return ERR_PTR(-EINVAL);
1266 
1267 	return __hwmon_sanitize_name(NULL, name);
1268 }
1269 EXPORT_SYMBOL_GPL(hwmon_sanitize_name);
1270 
1271 /**
1272  * devm_hwmon_sanitize_name - resource managed hwmon_sanitize_name()
1273  * @dev: device to allocate memory for
1274  * @name: NUL-terminated name
1275  *
1276  * Allocates a new string where any invalid characters will be replaced
1277  * by an underscore.
1278  *
1279  * Returns newly allocated name, or ERR_PTR on error.
1280  */
1281 char *devm_hwmon_sanitize_name(struct device *dev, const char *name)
1282 {
1283 	if (!dev || !name)
1284 		return ERR_PTR(-EINVAL);
1285 
1286 	return __hwmon_sanitize_name(dev, name);
1287 }
1288 EXPORT_SYMBOL_GPL(devm_hwmon_sanitize_name);
1289 
1290 static void __init hwmon_pci_quirks(void)
1291 {
1292 #if defined CONFIG_X86 && defined CONFIG_PCI
1293 	struct pci_dev *sb;
1294 	u16 base;
1295 	u8 enable;
1296 
1297 	/* Open access to 0x295-0x296 on MSI MS-7031 */
1298 	sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
1299 	if (sb) {
1300 		if (sb->subsystem_vendor == 0x1462 &&	/* MSI */
1301 		    sb->subsystem_device == 0x0031) {	/* MS-7031 */
1302 			pci_read_config_byte(sb, 0x48, &enable);
1303 			pci_read_config_word(sb, 0x64, &base);
1304 
1305 			if (base == 0 && !(enable & BIT(2))) {
1306 				dev_info(&sb->dev,
1307 					 "Opening wide generic port at 0x295\n");
1308 				pci_write_config_word(sb, 0x64, 0x295);
1309 				pci_write_config_byte(sb, 0x48,
1310 						      enable | BIT(2));
1311 			}
1312 		}
1313 		pci_dev_put(sb);
1314 	}
1315 #endif
1316 }
1317 
1318 static int __init hwmon_init(void)
1319 {
1320 	int err;
1321 
1322 	hwmon_pci_quirks();
1323 
1324 	err = class_register(&hwmon_class);
1325 	if (err) {
1326 		pr_err("couldn't register hwmon sysfs class\n");
1327 		return err;
1328 	}
1329 	return 0;
1330 }
1331 
1332 static void __exit hwmon_exit(void)
1333 {
1334 	class_unregister(&hwmon_class);
1335 }
1336 
1337 subsys_initcall(hwmon_init);
1338 module_exit(hwmon_exit);
1339 
1340 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1341 MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
1342 MODULE_LICENSE("GPL");
1343 
1344