xref: /linux/drivers/hwmon/hwmon.c (revision 2315332efcbe7124252f080e03b57d3d2f1f4771)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21236441fSMark M. Hoffman /*
35ed04880SGuenter Roeck  * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
45ed04880SGuenter Roeck  *
55ed04880SGuenter Roeck  * This file defines the sysfs class "hwmon", for use by sensors drivers.
65ed04880SGuenter Roeck  *
75ed04880SGuenter Roeck  * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
81236441fSMark M. Hoffman  */
91236441fSMark M. Hoffman 
10c95df1aeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11c95df1aeSJoe Perches 
12d560168bSGuenter Roeck #include <linux/bitops.h>
131236441fSMark M. Hoffman #include <linux/device.h>
141236441fSMark M. Hoffman #include <linux/err.h>
158c65b4a6STim Schmielau #include <linux/gfp.h>
16c9ebbe6fSGuenter Roeck #include <linux/hwmon.h>
17c9ebbe6fSGuenter Roeck #include <linux/idr.h>
1825f98688SChristophe JAILLET #include <linux/kstrtox.h>
191597b374SGuenter Roeck #include <linux/list.h>
20c9ebbe6fSGuenter Roeck #include <linux/module.h>
212958b1ecSJean Delvare #include <linux/pci.h>
22e1c9d6d6SPaul Cercueil #include <linux/property.h>
23c9ebbe6fSGuenter Roeck #include <linux/slab.h>
24648cd48cSGuenter Roeck #include <linux/string.h>
25d560168bSGuenter Roeck #include <linux/thermal.h>
261236441fSMark M. Hoffman 
2761b8ab2cSNicolin Chen #define CREATE_TRACE_POINTS
2861b8ab2cSNicolin Chen #include <trace/events/hwmon.h>
2961b8ab2cSNicolin Chen 
301236441fSMark M. Hoffman #define HWMON_ID_PREFIX "hwmon"
311236441fSMark M. Hoffman #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
321236441fSMark M. Hoffman 
33bab2243cSGuenter Roeck struct hwmon_device {
34bab2243cSGuenter Roeck 	const char *name;
35e1c9d6d6SPaul Cercueil 	const char *label;
36bab2243cSGuenter Roeck 	struct device dev;
37d560168bSGuenter Roeck 	const struct hwmon_chip_info *chip;
381597b374SGuenter Roeck 	struct list_head tzdata;
39d560168bSGuenter Roeck 	struct attribute_group group;
40d560168bSGuenter Roeck 	const struct attribute_group **groups;
41bab2243cSGuenter Roeck };
42d560168bSGuenter Roeck 
43bab2243cSGuenter Roeck #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
44bab2243cSGuenter Roeck 
453a412d5eSGuenter Roeck #define MAX_SYSFS_ATTR_NAME_LENGTH	32
463a412d5eSGuenter Roeck 
47d560168bSGuenter Roeck struct hwmon_device_attribute {
48d560168bSGuenter Roeck 	struct device_attribute dev_attr;
49d560168bSGuenter Roeck 	const struct hwmon_ops *ops;
50d560168bSGuenter Roeck 	enum hwmon_sensor_types type;
51d560168bSGuenter Roeck 	u32 attr;
52d560168bSGuenter Roeck 	int index;
533a412d5eSGuenter Roeck 	char name[MAX_SYSFS_ATTR_NAME_LENGTH];
54d560168bSGuenter Roeck };
55d560168bSGuenter Roeck 
56d560168bSGuenter Roeck #define to_hwmon_attr(d) \
57d560168bSGuenter Roeck 	container_of(d, struct hwmon_device_attribute, dev_attr)
583bf8bdcfSGuenter Roeck #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
59d560168bSGuenter Roeck 
60d560168bSGuenter Roeck /*
61d560168bSGuenter Roeck  * Thermal zone information
62d560168bSGuenter Roeck  */
63d560168bSGuenter Roeck struct hwmon_thermal_data {
641597b374SGuenter Roeck 	struct list_head node;		/* hwmon tzdata list entry */
653bf8bdcfSGuenter Roeck 	struct device *dev;		/* Reference to hwmon device */
66d560168bSGuenter Roeck 	int index;			/* sensor index */
671597b374SGuenter Roeck 	struct thermal_zone_device *tzd;/* thermal zone device */
68d560168bSGuenter Roeck };
69d560168bSGuenter Roeck 
70bab2243cSGuenter Roeck static ssize_t
712ab0c6c5SJulia Lawall name_show(struct device *dev, struct device_attribute *attr, char *buf)
72bab2243cSGuenter Roeck {
73bab2243cSGuenter Roeck 	return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
74bab2243cSGuenter Roeck }
752ab0c6c5SJulia Lawall static DEVICE_ATTR_RO(name);
76bab2243cSGuenter Roeck 
77e1c9d6d6SPaul Cercueil static ssize_t
78e1c9d6d6SPaul Cercueil label_show(struct device *dev, struct device_attribute *attr, char *buf)
79e1c9d6d6SPaul Cercueil {
80e1c9d6d6SPaul Cercueil 	return sysfs_emit(buf, "%s\n", to_hwmon_device(dev)->label);
81e1c9d6d6SPaul Cercueil }
82e1c9d6d6SPaul Cercueil static DEVICE_ATTR_RO(label);
83e1c9d6d6SPaul Cercueil 
84bab2243cSGuenter Roeck static struct attribute *hwmon_dev_attrs[] = {
85bab2243cSGuenter Roeck 	&dev_attr_name.attr,
86e1c9d6d6SPaul Cercueil 	&dev_attr_label.attr,
87bab2243cSGuenter Roeck 	NULL
88bab2243cSGuenter Roeck };
89bab2243cSGuenter Roeck 
90e1c9d6d6SPaul Cercueil static umode_t hwmon_dev_attr_is_visible(struct kobject *kobj,
91bab2243cSGuenter Roeck 					 struct attribute *attr, int n)
92bab2243cSGuenter Roeck {
9377d76768SYang Li 	struct device *dev = kobj_to_dev(kobj);
94e1c9d6d6SPaul Cercueil 	struct hwmon_device *hdev = to_hwmon_device(dev);
95bab2243cSGuenter Roeck 
96e1c9d6d6SPaul Cercueil 	if (attr == &dev_attr_name.attr && hdev->name == NULL)
97e1c9d6d6SPaul Cercueil 		return 0;
98e1c9d6d6SPaul Cercueil 
99e1c9d6d6SPaul Cercueil 	if (attr == &dev_attr_label.attr && hdev->label == NULL)
100bab2243cSGuenter Roeck 		return 0;
101bab2243cSGuenter Roeck 
102bab2243cSGuenter Roeck 	return attr->mode;
103bab2243cSGuenter Roeck }
104bab2243cSGuenter Roeck 
105524703acSArvind Yadav static const struct attribute_group hwmon_dev_attr_group = {
106bab2243cSGuenter Roeck 	.attrs		= hwmon_dev_attrs,
107e1c9d6d6SPaul Cercueil 	.is_visible	= hwmon_dev_attr_is_visible,
108bab2243cSGuenter Roeck };
109bab2243cSGuenter Roeck 
110bab2243cSGuenter Roeck static const struct attribute_group *hwmon_dev_attr_groups[] = {
111bab2243cSGuenter Roeck 	&hwmon_dev_attr_group,
112bab2243cSGuenter Roeck 	NULL
113bab2243cSGuenter Roeck };
114bab2243cSGuenter Roeck 
1153bf8bdcfSGuenter Roeck static void hwmon_free_attrs(struct attribute **attrs)
1163bf8bdcfSGuenter Roeck {
1173bf8bdcfSGuenter Roeck 	int i;
1183bf8bdcfSGuenter Roeck 
1193bf8bdcfSGuenter Roeck 	for (i = 0; attrs[i]; i++) {
1203bf8bdcfSGuenter Roeck 		struct device_attribute *dattr = to_dev_attr(attrs[i]);
1213bf8bdcfSGuenter Roeck 		struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr);
1223bf8bdcfSGuenter Roeck 
1233bf8bdcfSGuenter Roeck 		kfree(hattr);
1243bf8bdcfSGuenter Roeck 	}
1253bf8bdcfSGuenter Roeck 	kfree(attrs);
1263bf8bdcfSGuenter Roeck }
1273bf8bdcfSGuenter Roeck 
128bab2243cSGuenter Roeck static void hwmon_dev_release(struct device *dev)
129bab2243cSGuenter Roeck {
1303bf8bdcfSGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(dev);
1313bf8bdcfSGuenter Roeck 
1323bf8bdcfSGuenter Roeck 	if (hwdev->group.attrs)
1333bf8bdcfSGuenter Roeck 		hwmon_free_attrs(hwdev->group.attrs);
1343bf8bdcfSGuenter Roeck 	kfree(hwdev->groups);
135e1c9d6d6SPaul Cercueil 	kfree(hwdev->label);
1363bf8bdcfSGuenter Roeck 	kfree(hwdev);
137bab2243cSGuenter Roeck }
138bab2243cSGuenter Roeck 
139bab2243cSGuenter Roeck static struct class hwmon_class = {
140bab2243cSGuenter Roeck 	.name = "hwmon",
141bab2243cSGuenter Roeck 	.owner = THIS_MODULE,
142bab2243cSGuenter Roeck 	.dev_groups = hwmon_dev_attr_groups,
143bab2243cSGuenter Roeck 	.dev_release = hwmon_dev_release,
144bab2243cSGuenter Roeck };
1451236441fSMark M. Hoffman 
1464ca5f468SJonathan Cameron static DEFINE_IDA(hwmon_ida);
1471236441fSMark M. Hoffman 
148d560168bSGuenter Roeck /* Thermal zone handling */
149d560168bSGuenter Roeck 
15086430c1aSGuenter Roeck /*
15186430c1aSGuenter Roeck  * The complex conditional is necessary to avoid a cyclic dependency
15286430c1aSGuenter Roeck  * between hwmon and thermal_sys modules.
15386430c1aSGuenter Roeck  */
154f3735332SDaniel Lezcano #ifdef CONFIG_THERMAL_OF
155e5181331SDaniel Lezcano static int hwmon_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
156d560168bSGuenter Roeck {
157e5181331SDaniel Lezcano 	struct hwmon_thermal_data *tdata = tz->devdata;
1583bf8bdcfSGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
159d560168bSGuenter Roeck 	int ret;
160d560168bSGuenter Roeck 	long t;
161d560168bSGuenter Roeck 
1623bf8bdcfSGuenter Roeck 	ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
163d560168bSGuenter Roeck 				     tdata->index, &t);
164d560168bSGuenter Roeck 	if (ret < 0)
165d560168bSGuenter Roeck 		return ret;
166d560168bSGuenter Roeck 
167d560168bSGuenter Roeck 	*temp = t;
168d560168bSGuenter Roeck 
169d560168bSGuenter Roeck 	return 0;
170d560168bSGuenter Roeck }
171d560168bSGuenter Roeck 
172e5181331SDaniel Lezcano static int hwmon_thermal_set_trips(struct thermal_zone_device *tz, int low, int high)
173a5f6c0f8SDmitry Osipenko {
174e5181331SDaniel Lezcano 	struct hwmon_thermal_data *tdata = tz->devdata;
175a5f6c0f8SDmitry Osipenko 	struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
176a5f6c0f8SDmitry Osipenko 	const struct hwmon_chip_info *chip = hwdev->chip;
177a5f6c0f8SDmitry Osipenko 	const struct hwmon_channel_info **info = chip->info;
178a5f6c0f8SDmitry Osipenko 	unsigned int i;
179a5f6c0f8SDmitry Osipenko 	int err;
180a5f6c0f8SDmitry Osipenko 
181a5f6c0f8SDmitry Osipenko 	if (!chip->ops->write)
182a5f6c0f8SDmitry Osipenko 		return 0;
183a5f6c0f8SDmitry Osipenko 
184a5f6c0f8SDmitry Osipenko 	for (i = 0; info[i] && info[i]->type != hwmon_temp; i++)
185a5f6c0f8SDmitry Osipenko 		continue;
186a5f6c0f8SDmitry Osipenko 
187a5f6c0f8SDmitry Osipenko 	if (!info[i])
188a5f6c0f8SDmitry Osipenko 		return 0;
189a5f6c0f8SDmitry Osipenko 
190a5f6c0f8SDmitry Osipenko 	if (info[i]->config[tdata->index] & HWMON_T_MIN) {
191a5f6c0f8SDmitry Osipenko 		err = chip->ops->write(tdata->dev, hwmon_temp,
192a5f6c0f8SDmitry Osipenko 				       hwmon_temp_min, tdata->index, low);
193a5f6c0f8SDmitry Osipenko 		if (err && err != -EOPNOTSUPP)
194a5f6c0f8SDmitry Osipenko 			return err;
195a5f6c0f8SDmitry Osipenko 	}
196a5f6c0f8SDmitry Osipenko 
197a5f6c0f8SDmitry Osipenko 	if (info[i]->config[tdata->index] & HWMON_T_MAX) {
198a5f6c0f8SDmitry Osipenko 		err = chip->ops->write(tdata->dev, hwmon_temp,
199a5f6c0f8SDmitry Osipenko 				       hwmon_temp_max, tdata->index, high);
200a5f6c0f8SDmitry Osipenko 		if (err && err != -EOPNOTSUPP)
201a5f6c0f8SDmitry Osipenko 			return err;
202a5f6c0f8SDmitry Osipenko 	}
203a5f6c0f8SDmitry Osipenko 
204a5f6c0f8SDmitry Osipenko 	return 0;
205a5f6c0f8SDmitry Osipenko }
206a5f6c0f8SDmitry Osipenko 
207e5181331SDaniel Lezcano static const struct thermal_zone_device_ops hwmon_thermal_ops = {
208d560168bSGuenter Roeck 	.get_temp = hwmon_thermal_get_temp,
209a5f6c0f8SDmitry Osipenko 	.set_trips = hwmon_thermal_set_trips,
210d560168bSGuenter Roeck };
211d560168bSGuenter Roeck 
2121597b374SGuenter Roeck static void hwmon_thermal_remove_sensor(void *data)
2131597b374SGuenter Roeck {
2141597b374SGuenter Roeck 	list_del(data);
2151597b374SGuenter Roeck }
2161597b374SGuenter Roeck 
2173bf8bdcfSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, int index)
218d560168bSGuenter Roeck {
2191597b374SGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(dev);
220d560168bSGuenter Roeck 	struct hwmon_thermal_data *tdata;
22147c332deSLinus Walleij 	struct thermal_zone_device *tzd;
2221597b374SGuenter Roeck 	int err;
223d560168bSGuenter Roeck 
224d560168bSGuenter Roeck 	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
225d560168bSGuenter Roeck 	if (!tdata)
226d560168bSGuenter Roeck 		return -ENOMEM;
227d560168bSGuenter Roeck 
2283bf8bdcfSGuenter Roeck 	tdata->dev = dev;
229d560168bSGuenter Roeck 	tdata->index = index;
230d560168bSGuenter Roeck 
231e5181331SDaniel Lezcano 	tzd = devm_thermal_of_zone_register(dev, index, tdata,
232d560168bSGuenter Roeck 					    &hwmon_thermal_ops);
2331b5f517cSGuenter Roeck 	if (IS_ERR(tzd)) {
2341b5f517cSGuenter Roeck 		if (PTR_ERR(tzd) != -ENODEV)
23547c332deSLinus Walleij 			return PTR_ERR(tzd);
2361b5f517cSGuenter Roeck 		dev_info(dev, "temp%d_input not attached to any thermal zone\n",
2371b5f517cSGuenter Roeck 			 index + 1);
2381b5f517cSGuenter Roeck 		devm_kfree(dev, tdata);
2391b5f517cSGuenter Roeck 		return 0;
2401b5f517cSGuenter Roeck 	}
241d560168bSGuenter Roeck 
2421597b374SGuenter Roeck 	err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node);
2431597b374SGuenter Roeck 	if (err)
2441597b374SGuenter Roeck 		return err;
2451597b374SGuenter Roeck 
2461597b374SGuenter Roeck 	tdata->tzd = tzd;
2471597b374SGuenter Roeck 	list_add(&tdata->node, &hwdev->tzdata);
2481597b374SGuenter Roeck 
249d560168bSGuenter Roeck 	return 0;
250d560168bSGuenter Roeck }
25144e3ad88SAkinobu Mita 
25244e3ad88SAkinobu Mita static int hwmon_thermal_register_sensors(struct device *dev)
25344e3ad88SAkinobu Mita {
25444e3ad88SAkinobu Mita 	struct hwmon_device *hwdev = to_hwmon_device(dev);
25544e3ad88SAkinobu Mita 	const struct hwmon_chip_info *chip = hwdev->chip;
25644e3ad88SAkinobu Mita 	const struct hwmon_channel_info **info = chip->info;
25744e3ad88SAkinobu Mita 	void *drvdata = dev_get_drvdata(dev);
25844e3ad88SAkinobu Mita 	int i;
25944e3ad88SAkinobu Mita 
26044e3ad88SAkinobu Mita 	for (i = 1; info[i]; i++) {
26144e3ad88SAkinobu Mita 		int j;
26244e3ad88SAkinobu Mita 
26344e3ad88SAkinobu Mita 		if (info[i]->type != hwmon_temp)
26444e3ad88SAkinobu Mita 			continue;
26544e3ad88SAkinobu Mita 
26644e3ad88SAkinobu Mita 		for (j = 0; info[i]->config[j]; j++) {
26744e3ad88SAkinobu Mita 			int err;
26844e3ad88SAkinobu Mita 
26944e3ad88SAkinobu Mita 			if (!(info[i]->config[j] & HWMON_T_INPUT) ||
27044e3ad88SAkinobu Mita 			    !chip->ops->is_visible(drvdata, hwmon_temp,
27144e3ad88SAkinobu Mita 						   hwmon_temp_input, j))
27244e3ad88SAkinobu Mita 				continue;
27344e3ad88SAkinobu Mita 
27444e3ad88SAkinobu Mita 			err = hwmon_thermal_add_sensor(dev, j);
27544e3ad88SAkinobu Mita 			if (err)
27644e3ad88SAkinobu Mita 				return err;
27744e3ad88SAkinobu Mita 		}
27844e3ad88SAkinobu Mita 	}
27944e3ad88SAkinobu Mita 
28044e3ad88SAkinobu Mita 	return 0;
28144e3ad88SAkinobu Mita }
28244e3ad88SAkinobu Mita 
2831597b374SGuenter Roeck static void hwmon_thermal_notify(struct device *dev, int index)
2841597b374SGuenter Roeck {
2851597b374SGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(dev);
2861597b374SGuenter Roeck 	struct hwmon_thermal_data *tzdata;
2871597b374SGuenter Roeck 
2881597b374SGuenter Roeck 	list_for_each_entry(tzdata, &hwdev->tzdata, node) {
2891597b374SGuenter Roeck 		if (tzdata->index == index) {
2901597b374SGuenter Roeck 			thermal_zone_device_update(tzdata->tzd,
2911597b374SGuenter Roeck 						   THERMAL_EVENT_UNSPECIFIED);
2921597b374SGuenter Roeck 		}
2931597b374SGuenter Roeck 	}
2941597b374SGuenter Roeck }
2951597b374SGuenter Roeck 
296d560168bSGuenter Roeck #else
29744e3ad88SAkinobu Mita static int hwmon_thermal_register_sensors(struct device *dev)
298d560168bSGuenter Roeck {
299d560168bSGuenter Roeck 	return 0;
300d560168bSGuenter Roeck }
3011597b374SGuenter Roeck 
3021597b374SGuenter Roeck static void hwmon_thermal_notify(struct device *dev, int index) { }
3031597b374SGuenter Roeck 
30486430c1aSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
305d560168bSGuenter Roeck 
30661b8ab2cSNicolin Chen static int hwmon_attr_base(enum hwmon_sensor_types type)
30761b8ab2cSNicolin Chen {
3084413405fSDr. David Alan Gilbert 	if (type == hwmon_in || type == hwmon_intrusion)
30961b8ab2cSNicolin Chen 		return 0;
31061b8ab2cSNicolin Chen 	return 1;
31161b8ab2cSNicolin Chen }
31261b8ab2cSNicolin Chen 
313d560168bSGuenter Roeck /* sysfs attribute management */
314d560168bSGuenter Roeck 
315d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev,
316d560168bSGuenter Roeck 			       struct device_attribute *devattr, char *buf)
317d560168bSGuenter Roeck {
318d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
319d560168bSGuenter Roeck 	long val;
320d560168bSGuenter Roeck 	int ret;
321d560168bSGuenter Roeck 
322d560168bSGuenter Roeck 	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
323d560168bSGuenter Roeck 			       &val);
324d560168bSGuenter Roeck 	if (ret < 0)
325d560168bSGuenter Roeck 		return ret;
326d560168bSGuenter Roeck 
32761b8ab2cSNicolin Chen 	trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
32861b8ab2cSNicolin Chen 			      hattr->name, val);
32961b8ab2cSNicolin Chen 
330d560168bSGuenter Roeck 	return sprintf(buf, "%ld\n", val);
331d560168bSGuenter Roeck }
332d560168bSGuenter Roeck 
333e159ab5cSGuenter Roeck static ssize_t hwmon_attr_show_string(struct device *dev,
334e159ab5cSGuenter Roeck 				      struct device_attribute *devattr,
335e159ab5cSGuenter Roeck 				      char *buf)
336e159ab5cSGuenter Roeck {
337e159ab5cSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
33861b8ab2cSNicolin Chen 	enum hwmon_sensor_types type = hattr->type;
3395ba6bcbcSJean Delvare 	const char *s;
340e159ab5cSGuenter Roeck 	int ret;
341e159ab5cSGuenter Roeck 
342e159ab5cSGuenter Roeck 	ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
343e159ab5cSGuenter Roeck 				      hattr->index, &s);
344e159ab5cSGuenter Roeck 	if (ret < 0)
345e159ab5cSGuenter Roeck 		return ret;
346e159ab5cSGuenter Roeck 
34761b8ab2cSNicolin Chen 	trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
34861b8ab2cSNicolin Chen 				     hattr->name, s);
34961b8ab2cSNicolin Chen 
350e159ab5cSGuenter Roeck 	return sprintf(buf, "%s\n", s);
351e159ab5cSGuenter Roeck }
352e159ab5cSGuenter Roeck 
353d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev,
354d560168bSGuenter Roeck 				struct device_attribute *devattr,
355d560168bSGuenter Roeck 				const char *buf, size_t count)
356d560168bSGuenter Roeck {
357d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
358d560168bSGuenter Roeck 	long val;
359d560168bSGuenter Roeck 	int ret;
360d560168bSGuenter Roeck 
361d560168bSGuenter Roeck 	ret = kstrtol(buf, 10, &val);
362d560168bSGuenter Roeck 	if (ret < 0)
363d560168bSGuenter Roeck 		return ret;
364d560168bSGuenter Roeck 
365d560168bSGuenter Roeck 	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
366d560168bSGuenter Roeck 				val);
367d560168bSGuenter Roeck 	if (ret < 0)
368d560168bSGuenter Roeck 		return ret;
369d560168bSGuenter Roeck 
37061b8ab2cSNicolin Chen 	trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
37161b8ab2cSNicolin Chen 			       hattr->name, val);
372d560168bSGuenter Roeck 
37361b8ab2cSNicolin Chen 	return count;
374d560168bSGuenter Roeck }
375d560168bSGuenter Roeck 
376e159ab5cSGuenter Roeck static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
377e159ab5cSGuenter Roeck {
378e159ab5cSGuenter Roeck 	return (type == hwmon_temp && attr == hwmon_temp_label) ||
379e159ab5cSGuenter Roeck 	       (type == hwmon_in && attr == hwmon_in_label) ||
380e159ab5cSGuenter Roeck 	       (type == hwmon_curr && attr == hwmon_curr_label) ||
381e159ab5cSGuenter Roeck 	       (type == hwmon_power && attr == hwmon_power_label) ||
382e159ab5cSGuenter Roeck 	       (type == hwmon_energy && attr == hwmon_energy_label) ||
383e159ab5cSGuenter Roeck 	       (type == hwmon_humidity && attr == hwmon_humidity_label) ||
384e159ab5cSGuenter Roeck 	       (type == hwmon_fan && attr == hwmon_fan_label);
385e159ab5cSGuenter Roeck }
386e159ab5cSGuenter Roeck 
3873bf8bdcfSGuenter Roeck static struct attribute *hwmon_genattr(const void *drvdata,
388d560168bSGuenter Roeck 				       enum hwmon_sensor_types type,
389d560168bSGuenter Roeck 				       u32 attr,
390d560168bSGuenter Roeck 				       int index,
391d560168bSGuenter Roeck 				       const char *template,
392d560168bSGuenter Roeck 				       const struct hwmon_ops *ops)
393d560168bSGuenter Roeck {
394d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr;
395d560168bSGuenter Roeck 	struct device_attribute *dattr;
396d560168bSGuenter Roeck 	struct attribute *a;
397d560168bSGuenter Roeck 	umode_t mode;
3983b443defSRasmus Villemoes 	const char *name;
399e159ab5cSGuenter Roeck 	bool is_string = is_string_attr(type, attr);
400d560168bSGuenter Roeck 
401d560168bSGuenter Roeck 	/* The attribute is invisible if there is no template string */
402d560168bSGuenter Roeck 	if (!template)
403d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
404d560168bSGuenter Roeck 
405d560168bSGuenter Roeck 	mode = ops->is_visible(drvdata, type, attr, index);
406d560168bSGuenter Roeck 	if (!mode)
407d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
408d560168bSGuenter Roeck 
4090d87116fSGuenter Roeck 	if ((mode & 0444) && ((is_string && !ops->read_string) ||
410e159ab5cSGuenter Roeck 				 (!is_string && !ops->read)))
411d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
4120d87116fSGuenter Roeck 	if ((mode & 0222) && !ops->write)
413d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
414d560168bSGuenter Roeck 
4153bf8bdcfSGuenter Roeck 	hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
416d560168bSGuenter Roeck 	if (!hattr)
417d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
418d560168bSGuenter Roeck 
4193a412d5eSGuenter Roeck 	if (type == hwmon_chip) {
4203b443defSRasmus Villemoes 		name = template;
4213a412d5eSGuenter Roeck 	} else {
4223a412d5eSGuenter Roeck 		scnprintf(hattr->name, sizeof(hattr->name), template,
4233a412d5eSGuenter Roeck 			  index + hwmon_attr_base(type));
4243a412d5eSGuenter Roeck 		name = hattr->name;
4253a412d5eSGuenter Roeck 	}
4263a412d5eSGuenter Roeck 
427d560168bSGuenter Roeck 	hattr->type = type;
428d560168bSGuenter Roeck 	hattr->attr = attr;
429d560168bSGuenter Roeck 	hattr->index = index;
430d560168bSGuenter Roeck 	hattr->ops = ops;
431d560168bSGuenter Roeck 
432d560168bSGuenter Roeck 	dattr = &hattr->dev_attr;
433e159ab5cSGuenter Roeck 	dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
434d560168bSGuenter Roeck 	dattr->store = hwmon_attr_store;
435d560168bSGuenter Roeck 
436d560168bSGuenter Roeck 	a = &dattr->attr;
437d560168bSGuenter Roeck 	sysfs_attr_init(a);
438d560168bSGuenter Roeck 	a->name = name;
439d560168bSGuenter Roeck 	a->mode = mode;
440d560168bSGuenter Roeck 
441d560168bSGuenter Roeck 	return a;
442d560168bSGuenter Roeck }
443d560168bSGuenter Roeck 
444f4d325d5SGuenter Roeck /*
445f4d325d5SGuenter Roeck  * Chip attributes are not attribute templates but actual sysfs attributes.
446f4d325d5SGuenter Roeck  * See hwmon_genattr() for special handling.
447f4d325d5SGuenter Roeck  */
448f4d325d5SGuenter Roeck static const char * const hwmon_chip_attrs[] = {
449d560168bSGuenter Roeck 	[hwmon_chip_temp_reset_history] = "temp_reset_history",
45000d616cfSGuenter Roeck 	[hwmon_chip_in_reset_history] = "in_reset_history",
4519b26947cSGuenter Roeck 	[hwmon_chip_curr_reset_history] = "curr_reset_history",
452b308f5c7SGuenter Roeck 	[hwmon_chip_power_reset_history] = "power_reset_history",
453d560168bSGuenter Roeck 	[hwmon_chip_update_interval] = "update_interval",
454d560168bSGuenter Roeck 	[hwmon_chip_alarms] = "alarms",
4559f00995eSGuenter Roeck 	[hwmon_chip_samples] = "samples",
4569f00995eSGuenter Roeck 	[hwmon_chip_curr_samples] = "curr_samples",
4579f00995eSGuenter Roeck 	[hwmon_chip_in_samples] = "in_samples",
4589f00995eSGuenter Roeck 	[hwmon_chip_power_samples] = "power_samples",
4599f00995eSGuenter Roeck 	[hwmon_chip_temp_samples] = "temp_samples",
460d560168bSGuenter Roeck };
461d560168bSGuenter Roeck 
462d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = {
463002c6b54SGuenter Roeck 	[hwmon_temp_enable] = "temp%d_enable",
464d560168bSGuenter Roeck 	[hwmon_temp_input] = "temp%d_input",
465d560168bSGuenter Roeck 	[hwmon_temp_type] = "temp%d_type",
466d560168bSGuenter Roeck 	[hwmon_temp_lcrit] = "temp%d_lcrit",
467d560168bSGuenter Roeck 	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
468d560168bSGuenter Roeck 	[hwmon_temp_min] = "temp%d_min",
469d560168bSGuenter Roeck 	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
470d560168bSGuenter Roeck 	[hwmon_temp_max] = "temp%d_max",
471d560168bSGuenter Roeck 	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
472d560168bSGuenter Roeck 	[hwmon_temp_crit] = "temp%d_crit",
473d560168bSGuenter Roeck 	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
474d560168bSGuenter Roeck 	[hwmon_temp_emergency] = "temp%d_emergency",
475d560168bSGuenter Roeck 	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
476d560168bSGuenter Roeck 	[hwmon_temp_alarm] = "temp%d_alarm",
477d560168bSGuenter Roeck 	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
478d560168bSGuenter Roeck 	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
479d560168bSGuenter Roeck 	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
480d560168bSGuenter Roeck 	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
481d560168bSGuenter Roeck 	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
482d560168bSGuenter Roeck 	[hwmon_temp_fault] = "temp%d_fault",
483d560168bSGuenter Roeck 	[hwmon_temp_offset] = "temp%d_offset",
484d560168bSGuenter Roeck 	[hwmon_temp_label] = "temp%d_label",
485d560168bSGuenter Roeck 	[hwmon_temp_lowest] = "temp%d_lowest",
486d560168bSGuenter Roeck 	[hwmon_temp_highest] = "temp%d_highest",
487d560168bSGuenter Roeck 	[hwmon_temp_reset_history] = "temp%d_reset_history",
4881967f712SZbigniew Lukwinski 	[hwmon_temp_rated_min] = "temp%d_rated_min",
4891967f712SZbigniew Lukwinski 	[hwmon_temp_rated_max] = "temp%d_rated_max",
490d560168bSGuenter Roeck };
491d560168bSGuenter Roeck 
49200d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = {
493002c6b54SGuenter Roeck 	[hwmon_in_enable] = "in%d_enable",
49400d616cfSGuenter Roeck 	[hwmon_in_input] = "in%d_input",
49500d616cfSGuenter Roeck 	[hwmon_in_min] = "in%d_min",
49600d616cfSGuenter Roeck 	[hwmon_in_max] = "in%d_max",
49700d616cfSGuenter Roeck 	[hwmon_in_lcrit] = "in%d_lcrit",
49800d616cfSGuenter Roeck 	[hwmon_in_crit] = "in%d_crit",
49900d616cfSGuenter Roeck 	[hwmon_in_average] = "in%d_average",
50000d616cfSGuenter Roeck 	[hwmon_in_lowest] = "in%d_lowest",
50100d616cfSGuenter Roeck 	[hwmon_in_highest] = "in%d_highest",
50200d616cfSGuenter Roeck 	[hwmon_in_reset_history] = "in%d_reset_history",
50300d616cfSGuenter Roeck 	[hwmon_in_label] = "in%d_label",
50400d616cfSGuenter Roeck 	[hwmon_in_alarm] = "in%d_alarm",
50500d616cfSGuenter Roeck 	[hwmon_in_min_alarm] = "in%d_min_alarm",
50600d616cfSGuenter Roeck 	[hwmon_in_max_alarm] = "in%d_max_alarm",
50700d616cfSGuenter Roeck 	[hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
50800d616cfSGuenter Roeck 	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
5091967f712SZbigniew Lukwinski 	[hwmon_in_rated_min] = "in%d_rated_min",
5101967f712SZbigniew Lukwinski 	[hwmon_in_rated_max] = "in%d_rated_max",
51100d616cfSGuenter Roeck };
51200d616cfSGuenter Roeck 
5139b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = {
514002c6b54SGuenter Roeck 	[hwmon_curr_enable] = "curr%d_enable",
5159b26947cSGuenter Roeck 	[hwmon_curr_input] = "curr%d_input",
5169b26947cSGuenter Roeck 	[hwmon_curr_min] = "curr%d_min",
5179b26947cSGuenter Roeck 	[hwmon_curr_max] = "curr%d_max",
5189b26947cSGuenter Roeck 	[hwmon_curr_lcrit] = "curr%d_lcrit",
5199b26947cSGuenter Roeck 	[hwmon_curr_crit] = "curr%d_crit",
5209b26947cSGuenter Roeck 	[hwmon_curr_average] = "curr%d_average",
5219b26947cSGuenter Roeck 	[hwmon_curr_lowest] = "curr%d_lowest",
5229b26947cSGuenter Roeck 	[hwmon_curr_highest] = "curr%d_highest",
5239b26947cSGuenter Roeck 	[hwmon_curr_reset_history] = "curr%d_reset_history",
5249b26947cSGuenter Roeck 	[hwmon_curr_label] = "curr%d_label",
5259b26947cSGuenter Roeck 	[hwmon_curr_alarm] = "curr%d_alarm",
5269b26947cSGuenter Roeck 	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
5279b26947cSGuenter Roeck 	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
5289b26947cSGuenter Roeck 	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
5299b26947cSGuenter Roeck 	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
5301967f712SZbigniew Lukwinski 	[hwmon_curr_rated_min] = "curr%d_rated_min",
5311967f712SZbigniew Lukwinski 	[hwmon_curr_rated_max] = "curr%d_rated_max",
5329b26947cSGuenter Roeck };
5339b26947cSGuenter Roeck 
534b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = {
535002c6b54SGuenter Roeck 	[hwmon_power_enable] = "power%d_enable",
536b308f5c7SGuenter Roeck 	[hwmon_power_average] = "power%d_average",
537b308f5c7SGuenter Roeck 	[hwmon_power_average_interval] = "power%d_average_interval",
538b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_max] = "power%d_interval_max",
539b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_min] = "power%d_interval_min",
540b308f5c7SGuenter Roeck 	[hwmon_power_average_highest] = "power%d_average_highest",
541b308f5c7SGuenter Roeck 	[hwmon_power_average_lowest] = "power%d_average_lowest",
542b308f5c7SGuenter Roeck 	[hwmon_power_average_max] = "power%d_average_max",
543b308f5c7SGuenter Roeck 	[hwmon_power_average_min] = "power%d_average_min",
544b308f5c7SGuenter Roeck 	[hwmon_power_input] = "power%d_input",
545b308f5c7SGuenter Roeck 	[hwmon_power_input_highest] = "power%d_input_highest",
546b308f5c7SGuenter Roeck 	[hwmon_power_input_lowest] = "power%d_input_lowest",
547b308f5c7SGuenter Roeck 	[hwmon_power_reset_history] = "power%d_reset_history",
548b308f5c7SGuenter Roeck 	[hwmon_power_accuracy] = "power%d_accuracy",
549b308f5c7SGuenter Roeck 	[hwmon_power_cap] = "power%d_cap",
550b308f5c7SGuenter Roeck 	[hwmon_power_cap_hyst] = "power%d_cap_hyst",
551b308f5c7SGuenter Roeck 	[hwmon_power_cap_max] = "power%d_cap_max",
552b308f5c7SGuenter Roeck 	[hwmon_power_cap_min] = "power%d_cap_min",
553aa7f29b0SAndrew Lunn 	[hwmon_power_min] = "power%d_min",
554b308f5c7SGuenter Roeck 	[hwmon_power_max] = "power%d_max",
555aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit] = "power%d_lcrit",
556b308f5c7SGuenter Roeck 	[hwmon_power_crit] = "power%d_crit",
557b308f5c7SGuenter Roeck 	[hwmon_power_label] = "power%d_label",
558b308f5c7SGuenter Roeck 	[hwmon_power_alarm] = "power%d_alarm",
559b308f5c7SGuenter Roeck 	[hwmon_power_cap_alarm] = "power%d_cap_alarm",
560aa7f29b0SAndrew Lunn 	[hwmon_power_min_alarm] = "power%d_min_alarm",
561b308f5c7SGuenter Roeck 	[hwmon_power_max_alarm] = "power%d_max_alarm",
562aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
563b308f5c7SGuenter Roeck 	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
5641967f712SZbigniew Lukwinski 	[hwmon_power_rated_min] = "power%d_rated_min",
5651967f712SZbigniew Lukwinski 	[hwmon_power_rated_max] = "power%d_rated_max",
566b308f5c7SGuenter Roeck };
567b308f5c7SGuenter Roeck 
5686bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = {
569002c6b54SGuenter Roeck 	[hwmon_energy_enable] = "energy%d_enable",
5706bfcca44SGuenter Roeck 	[hwmon_energy_input] = "energy%d_input",
5716bfcca44SGuenter Roeck 	[hwmon_energy_label] = "energy%d_label",
5726bfcca44SGuenter Roeck };
5736bfcca44SGuenter Roeck 
5746bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = {
575002c6b54SGuenter Roeck 	[hwmon_humidity_enable] = "humidity%d_enable",
5766bfcca44SGuenter Roeck 	[hwmon_humidity_input] = "humidity%d_input",
5776bfcca44SGuenter Roeck 	[hwmon_humidity_label] = "humidity%d_label",
5786bfcca44SGuenter Roeck 	[hwmon_humidity_min] = "humidity%d_min",
5796bfcca44SGuenter Roeck 	[hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
5806bfcca44SGuenter Roeck 	[hwmon_humidity_max] = "humidity%d_max",
5816bfcca44SGuenter Roeck 	[hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
5826bfcca44SGuenter Roeck 	[hwmon_humidity_alarm] = "humidity%d_alarm",
5836bfcca44SGuenter Roeck 	[hwmon_humidity_fault] = "humidity%d_fault",
5841967f712SZbigniew Lukwinski 	[hwmon_humidity_rated_min] = "humidity%d_rated_min",
5851967f712SZbigniew Lukwinski 	[hwmon_humidity_rated_max] = "humidity%d_rated_max",
5866bfcca44SGuenter Roeck };
5876bfcca44SGuenter Roeck 
5888faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = {
589002c6b54SGuenter Roeck 	[hwmon_fan_enable] = "fan%d_enable",
5908faee73fSGuenter Roeck 	[hwmon_fan_input] = "fan%d_input",
5918faee73fSGuenter Roeck 	[hwmon_fan_label] = "fan%d_label",
5928faee73fSGuenter Roeck 	[hwmon_fan_min] = "fan%d_min",
5938faee73fSGuenter Roeck 	[hwmon_fan_max] = "fan%d_max",
5948faee73fSGuenter Roeck 	[hwmon_fan_div] = "fan%d_div",
5958faee73fSGuenter Roeck 	[hwmon_fan_pulses] = "fan%d_pulses",
5968faee73fSGuenter Roeck 	[hwmon_fan_target] = "fan%d_target",
5978faee73fSGuenter Roeck 	[hwmon_fan_alarm] = "fan%d_alarm",
5988faee73fSGuenter Roeck 	[hwmon_fan_min_alarm] = "fan%d_min_alarm",
5998faee73fSGuenter Roeck 	[hwmon_fan_max_alarm] = "fan%d_max_alarm",
6008faee73fSGuenter Roeck 	[hwmon_fan_fault] = "fan%d_fault",
6018faee73fSGuenter Roeck };
6028faee73fSGuenter Roeck 
603f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = {
604f9f7bb3aSGuenter Roeck 	[hwmon_pwm_input] = "pwm%d",
605f9f7bb3aSGuenter Roeck 	[hwmon_pwm_enable] = "pwm%d_enable",
606f9f7bb3aSGuenter Roeck 	[hwmon_pwm_mode] = "pwm%d_mode",
607f9f7bb3aSGuenter Roeck 	[hwmon_pwm_freq] = "pwm%d_freq",
608e75d16e5SArmin Wolf 	[hwmon_pwm_auto_channels_temp] = "pwm%d_auto_channels_temp",
609f9f7bb3aSGuenter Roeck };
610f9f7bb3aSGuenter Roeck 
6114413405fSDr. David Alan Gilbert static const char * const hwmon_intrusion_attr_templates[] = {
6124413405fSDr. David Alan Gilbert 	[hwmon_intrusion_alarm] = "intrusion%d_alarm",
6134413405fSDr. David Alan Gilbert 	[hwmon_intrusion_beep]  = "intrusion%d_beep",
6144413405fSDr. David Alan Gilbert };
6154413405fSDr. David Alan Gilbert 
616d560168bSGuenter Roeck static const char * const *__templates[] = {
617f4d325d5SGuenter Roeck 	[hwmon_chip] = hwmon_chip_attrs,
618d560168bSGuenter Roeck 	[hwmon_temp] = hwmon_temp_attr_templates,
61900d616cfSGuenter Roeck 	[hwmon_in] = hwmon_in_attr_templates,
6209b26947cSGuenter Roeck 	[hwmon_curr] = hwmon_curr_attr_templates,
621b308f5c7SGuenter Roeck 	[hwmon_power] = hwmon_power_attr_templates,
6226bfcca44SGuenter Roeck 	[hwmon_energy] = hwmon_energy_attr_templates,
6236bfcca44SGuenter Roeck 	[hwmon_humidity] = hwmon_humidity_attr_templates,
6248faee73fSGuenter Roeck 	[hwmon_fan] = hwmon_fan_attr_templates,
625f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = hwmon_pwm_attr_templates,
6264413405fSDr. David Alan Gilbert 	[hwmon_intrusion] = hwmon_intrusion_attr_templates,
627d560168bSGuenter Roeck };
628d560168bSGuenter Roeck 
629d560168bSGuenter Roeck static const int __templates_size[] = {
630f4d325d5SGuenter Roeck 	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
631d560168bSGuenter Roeck 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
63200d616cfSGuenter Roeck 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
6339b26947cSGuenter Roeck 	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
634b308f5c7SGuenter Roeck 	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
6356bfcca44SGuenter Roeck 	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
6366bfcca44SGuenter Roeck 	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
6378faee73fSGuenter Roeck 	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
638f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
6394413405fSDr. David Alan Gilbert 	[hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
640d560168bSGuenter Roeck };
641d560168bSGuenter Roeck 
6421597b374SGuenter Roeck int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
6431597b374SGuenter Roeck 		       u32 attr, int channel)
6441597b374SGuenter Roeck {
6457f3cc8f8SGuenter Roeck 	char event[MAX_SYSFS_ATTR_NAME_LENGTH + 5];
6461597b374SGuenter Roeck 	char sattr[MAX_SYSFS_ATTR_NAME_LENGTH];
6477f3cc8f8SGuenter Roeck 	char *envp[] = { event, NULL };
6481597b374SGuenter Roeck 	const char * const *templates;
6491597b374SGuenter Roeck 	const char *template;
6501597b374SGuenter Roeck 	int base;
6511597b374SGuenter Roeck 
6521597b374SGuenter Roeck 	if (type >= ARRAY_SIZE(__templates))
6531597b374SGuenter Roeck 		return -EINVAL;
6541597b374SGuenter Roeck 	if (attr >= __templates_size[type])
6551597b374SGuenter Roeck 		return -EINVAL;
6561597b374SGuenter Roeck 
6571597b374SGuenter Roeck 	templates = __templates[type];
6581597b374SGuenter Roeck 	template = templates[attr];
6591597b374SGuenter Roeck 
6601597b374SGuenter Roeck 	base = hwmon_attr_base(type);
6611597b374SGuenter Roeck 
6621597b374SGuenter Roeck 	scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel);
6637f3cc8f8SGuenter Roeck 	scnprintf(event, sizeof(event), "NAME=%s", sattr);
6641597b374SGuenter Roeck 	sysfs_notify(&dev->kobj, NULL, sattr);
6657f3cc8f8SGuenter Roeck 	kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
6661597b374SGuenter Roeck 
6671597b374SGuenter Roeck 	if (type == hwmon_temp)
6681597b374SGuenter Roeck 		hwmon_thermal_notify(dev, channel);
6691597b374SGuenter Roeck 
6701597b374SGuenter Roeck 	return 0;
6711597b374SGuenter Roeck }
6721597b374SGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_notify_event);
6731597b374SGuenter Roeck 
674d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
675d560168bSGuenter Roeck {
676d560168bSGuenter Roeck 	int i, n;
677d560168bSGuenter Roeck 
678d560168bSGuenter Roeck 	for (i = n = 0; info->config[i]; i++)
679d560168bSGuenter Roeck 		n += hweight32(info->config[i]);
680d560168bSGuenter Roeck 
681d560168bSGuenter Roeck 	return n;
682d560168bSGuenter Roeck }
683d560168bSGuenter Roeck 
6843bf8bdcfSGuenter Roeck static int hwmon_genattrs(const void *drvdata,
685d560168bSGuenter Roeck 			  struct attribute **attrs,
686d560168bSGuenter Roeck 			  const struct hwmon_ops *ops,
687d560168bSGuenter Roeck 			  const struct hwmon_channel_info *info)
688d560168bSGuenter Roeck {
689d560168bSGuenter Roeck 	const char * const *templates;
690d560168bSGuenter Roeck 	int template_size;
691d560168bSGuenter Roeck 	int i, aindex = 0;
692d560168bSGuenter Roeck 
693d560168bSGuenter Roeck 	if (info->type >= ARRAY_SIZE(__templates))
694d560168bSGuenter Roeck 		return -EINVAL;
695d560168bSGuenter Roeck 
696d560168bSGuenter Roeck 	templates = __templates[info->type];
697d560168bSGuenter Roeck 	template_size = __templates_size[info->type];
698d560168bSGuenter Roeck 
699d560168bSGuenter Roeck 	for (i = 0; info->config[i]; i++) {
700d560168bSGuenter Roeck 		u32 attr_mask = info->config[i];
701d560168bSGuenter Roeck 		u32 attr;
702d560168bSGuenter Roeck 
703d560168bSGuenter Roeck 		while (attr_mask) {
704d560168bSGuenter Roeck 			struct attribute *a;
705d560168bSGuenter Roeck 
706d560168bSGuenter Roeck 			attr = __ffs(attr_mask);
707d560168bSGuenter Roeck 			attr_mask &= ~BIT(attr);
708d560168bSGuenter Roeck 			if (attr >= template_size)
709d560168bSGuenter Roeck 				return -EINVAL;
7103bf8bdcfSGuenter Roeck 			a = hwmon_genattr(drvdata, info->type, attr, i,
711d560168bSGuenter Roeck 					  templates[attr], ops);
712d560168bSGuenter Roeck 			if (IS_ERR(a)) {
713d560168bSGuenter Roeck 				if (PTR_ERR(a) != -ENOENT)
714d560168bSGuenter Roeck 					return PTR_ERR(a);
715d560168bSGuenter Roeck 				continue;
716d560168bSGuenter Roeck 			}
717d560168bSGuenter Roeck 			attrs[aindex++] = a;
718d560168bSGuenter Roeck 		}
719d560168bSGuenter Roeck 	}
720d560168bSGuenter Roeck 	return aindex;
721d560168bSGuenter Roeck }
722d560168bSGuenter Roeck 
723d560168bSGuenter Roeck static struct attribute **
7243bf8bdcfSGuenter Roeck __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
725d560168bSGuenter Roeck {
726d560168bSGuenter Roeck 	int ret, i, aindex = 0, nattrs = 0;
727d560168bSGuenter Roeck 	struct attribute **attrs;
728d560168bSGuenter Roeck 
729d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++)
730d560168bSGuenter Roeck 		nattrs += hwmon_num_channel_attrs(chip->info[i]);
731d560168bSGuenter Roeck 
732d560168bSGuenter Roeck 	if (nattrs == 0)
733d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
734d560168bSGuenter Roeck 
7353bf8bdcfSGuenter Roeck 	attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
736d560168bSGuenter Roeck 	if (!attrs)
737d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
738d560168bSGuenter Roeck 
739d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++) {
7403bf8bdcfSGuenter Roeck 		ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
741d560168bSGuenter Roeck 				     chip->info[i]);
7423bf8bdcfSGuenter Roeck 		if (ret < 0) {
7433bf8bdcfSGuenter Roeck 			hwmon_free_attrs(attrs);
744d560168bSGuenter Roeck 			return ERR_PTR(ret);
7453bf8bdcfSGuenter Roeck 		}
746d560168bSGuenter Roeck 		aindex += ret;
747d560168bSGuenter Roeck 	}
748d560168bSGuenter Roeck 
749d560168bSGuenter Roeck 	return attrs;
750d560168bSGuenter Roeck }
751d560168bSGuenter Roeck 
752d560168bSGuenter Roeck static struct device *
753d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
754d560168bSGuenter Roeck 			const struct hwmon_chip_info *chip,
755d560168bSGuenter Roeck 			const struct attribute_group **groups)
756d560168bSGuenter Roeck {
757d560168bSGuenter Roeck 	struct hwmon_device *hwdev;
758e1c9d6d6SPaul Cercueil 	const char *label;
759d560168bSGuenter Roeck 	struct device *hdev;
760*2315332eSPhinex Hung 	struct device *tdev = dev;
76144e3ad88SAkinobu Mita 	int i, err, id;
762d560168bSGuenter Roeck 
76374d3b641SGuenter Roeck 	/* Complain about invalid characters in hwmon name attribute */
764d560168bSGuenter Roeck 	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
76574d3b641SGuenter Roeck 		dev_warn(dev,
76674d3b641SGuenter Roeck 			 "hwmon: '%s' is not a valid name attribute, please fix\n",
76774d3b641SGuenter Roeck 			 name);
768d560168bSGuenter Roeck 
769718fbfa5Skeliu 	id = ida_alloc(&hwmon_ida, GFP_KERNEL);
770d560168bSGuenter Roeck 	if (id < 0)
771d560168bSGuenter Roeck 		return ERR_PTR(id);
772d560168bSGuenter Roeck 
773d560168bSGuenter Roeck 	hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
774d560168bSGuenter Roeck 	if (hwdev == NULL) {
775d560168bSGuenter Roeck 		err = -ENOMEM;
776d560168bSGuenter Roeck 		goto ida_remove;
777d560168bSGuenter Roeck 	}
778d560168bSGuenter Roeck 
779d560168bSGuenter Roeck 	hdev = &hwdev->dev;
780d560168bSGuenter Roeck 
781239552f4SGuenter Roeck 	if (chip) {
782d560168bSGuenter Roeck 		struct attribute **attrs;
783b2a4cc3aSGuenter Roeck 		int ngroups = 2; /* terminating NULL plus &hwdev->groups */
784d560168bSGuenter Roeck 
785d560168bSGuenter Roeck 		if (groups)
786d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
787d560168bSGuenter Roeck 				ngroups++;
788d560168bSGuenter Roeck 
7893bf8bdcfSGuenter Roeck 		hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
79038d8ed65SColin Ian King 		if (!hwdev->groups) {
79138d8ed65SColin Ian King 			err = -ENOMEM;
79238d8ed65SColin Ian King 			goto free_hwmon;
79338d8ed65SColin Ian King 		}
794d560168bSGuenter Roeck 
7953bf8bdcfSGuenter Roeck 		attrs = __hwmon_create_attrs(drvdata, chip);
796d560168bSGuenter Roeck 		if (IS_ERR(attrs)) {
797d560168bSGuenter Roeck 			err = PTR_ERR(attrs);
798d560168bSGuenter Roeck 			goto free_hwmon;
799d560168bSGuenter Roeck 		}
800d560168bSGuenter Roeck 
801d560168bSGuenter Roeck 		hwdev->group.attrs = attrs;
802d560168bSGuenter Roeck 		ngroups = 0;
803d560168bSGuenter Roeck 		hwdev->groups[ngroups++] = &hwdev->group;
804d560168bSGuenter Roeck 
805d560168bSGuenter Roeck 		if (groups) {
806d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
807d560168bSGuenter Roeck 				hwdev->groups[ngroups++] = groups[i];
808d560168bSGuenter Roeck 		}
809d560168bSGuenter Roeck 
810d560168bSGuenter Roeck 		hdev->groups = hwdev->groups;
811d560168bSGuenter Roeck 	} else {
812d560168bSGuenter Roeck 		hdev->groups = groups;
813d560168bSGuenter Roeck 	}
814d560168bSGuenter Roeck 
81507320c91SPaul Cercueil 	if (dev && device_property_present(dev, "label")) {
816e1c9d6d6SPaul Cercueil 		err = device_property_read_string(dev, "label", &label);
817e1c9d6d6SPaul Cercueil 		if (err < 0)
818e1c9d6d6SPaul Cercueil 			goto free_hwmon;
819e1c9d6d6SPaul Cercueil 
820e1c9d6d6SPaul Cercueil 		hwdev->label = kstrdup(label, GFP_KERNEL);
821e1c9d6d6SPaul Cercueil 		if (hwdev->label == NULL) {
822e1c9d6d6SPaul Cercueil 			err = -ENOMEM;
823e1c9d6d6SPaul Cercueil 			goto free_hwmon;
824e1c9d6d6SPaul Cercueil 		}
825e1c9d6d6SPaul Cercueil 	}
826e1c9d6d6SPaul Cercueil 
827d560168bSGuenter Roeck 	hwdev->name = name;
828d560168bSGuenter Roeck 	hdev->class = &hwmon_class;
829d560168bSGuenter Roeck 	hdev->parent = dev;
830*2315332eSPhinex Hung 	while (tdev && !tdev->of_node)
831*2315332eSPhinex Hung 		tdev = tdev->parent;
832*2315332eSPhinex Hung 	hdev->of_node = tdev ? tdev->of_node : NULL;
833d560168bSGuenter Roeck 	hwdev->chip = chip;
834d560168bSGuenter Roeck 	dev_set_drvdata(hdev, drvdata);
835d560168bSGuenter Roeck 	dev_set_name(hdev, HWMON_ID_FORMAT, id);
836d560168bSGuenter Roeck 	err = device_register(hdev);
837ada61aa0SYang Yingliang 	if (err) {
838ada61aa0SYang Yingliang 		put_device(hdev);
839ada61aa0SYang Yingliang 		goto ida_remove;
840ada61aa0SYang Yingliang 	}
841d560168bSGuenter Roeck 
8421597b374SGuenter Roeck 	INIT_LIST_HEAD(&hwdev->tzdata);
8431597b374SGuenter Roeck 
844*2315332eSPhinex Hung 	if (hdev->of_node && chip && chip->ops->read &&
845d560168bSGuenter Roeck 	    chip->info[0]->type == hwmon_chip &&
846d560168bSGuenter Roeck 	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
84744e3ad88SAkinobu Mita 		err = hwmon_thermal_register_sensors(hdev);
84874e35127SDmitry Osipenko 		if (err) {
84974e35127SDmitry Osipenko 			device_unregister(hdev);
850792eac18SGuenter Roeck 			/*
85144e3ad88SAkinobu Mita 			 * Don't worry about hwdev; hwmon_dev_release(), called
85244e3ad88SAkinobu Mita 			 * from device_unregister(), will free it.
853792eac18SGuenter Roeck 			 */
85474e35127SDmitry Osipenko 			goto ida_remove;
85574e35127SDmitry Osipenko 		}
85647c332deSLinus Walleij 	}
857d560168bSGuenter Roeck 
858d560168bSGuenter Roeck 	return hdev;
859d560168bSGuenter Roeck 
860d560168bSGuenter Roeck free_hwmon:
8613bf8bdcfSGuenter Roeck 	hwmon_dev_release(hdev);
862d560168bSGuenter Roeck ida_remove:
863718fbfa5Skeliu 	ida_free(&hwmon_ida, id);
864d560168bSGuenter Roeck 	return ERR_PTR(err);
865d560168bSGuenter Roeck }
866d560168bSGuenter Roeck 
8671236441fSMark M. Hoffman /**
868bab2243cSGuenter Roeck  * hwmon_device_register_with_groups - register w/ hwmon
869bab2243cSGuenter Roeck  * @dev: the parent device
870bab2243cSGuenter Roeck  * @name: hwmon name attribute
871bab2243cSGuenter Roeck  * @drvdata: driver data to attach to created device
872bab2243cSGuenter Roeck  * @groups: List of attribute groups to create
873bab2243cSGuenter Roeck  *
874bab2243cSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
875bab2243cSGuenter Roeck  * longer needed.
876bab2243cSGuenter Roeck  *
877bab2243cSGuenter Roeck  * Returns the pointer to the new device.
878bab2243cSGuenter Roeck  */
879bab2243cSGuenter Roeck struct device *
880bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name,
881bab2243cSGuenter Roeck 				  void *drvdata,
882bab2243cSGuenter Roeck 				  const struct attribute_group **groups)
883bab2243cSGuenter Roeck {
8848353863aSGuenter Roeck 	if (!name)
8858353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
8868353863aSGuenter Roeck 
887d560168bSGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
888bab2243cSGuenter Roeck }
889bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
890bab2243cSGuenter Roeck 
891bab2243cSGuenter Roeck /**
892d560168bSGuenter Roeck  * hwmon_device_register_with_info - register w/ hwmon
893ddaefa20SGuenter Roeck  * @dev: the parent device (mandatory)
894ddaefa20SGuenter Roeck  * @name: hwmon name attribute (mandatory)
895ddaefa20SGuenter Roeck  * @drvdata: driver data to attach to created device (optional)
896ddaefa20SGuenter Roeck  * @chip: pointer to hwmon chip information (mandatory)
897848ba0a2SGuenter Roeck  * @extra_groups: pointer to list of additional non-standard attribute groups
898ddaefa20SGuenter Roeck  *	(optional)
899d560168bSGuenter Roeck  *
900d560168bSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
901d560168bSGuenter Roeck  * longer needed.
902d560168bSGuenter Roeck  *
903d560168bSGuenter Roeck  * Returns the pointer to the new device.
904d560168bSGuenter Roeck  */
905d560168bSGuenter Roeck struct device *
906d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name,
907d560168bSGuenter Roeck 				void *drvdata,
908d560168bSGuenter Roeck 				const struct hwmon_chip_info *chip,
909848ba0a2SGuenter Roeck 				const struct attribute_group **extra_groups)
910d560168bSGuenter Roeck {
911ddaefa20SGuenter Roeck 	if (!dev || !name || !chip)
9128353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
9138353863aSGuenter Roeck 
914ddaefa20SGuenter Roeck 	if (!chip->ops || !chip->ops->is_visible || !chip->info)
91559df4f4eSLucas Magasweran 		return ERR_PTR(-EINVAL);
91659df4f4eSLucas Magasweran 
917848ba0a2SGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
918d560168bSGuenter Roeck }
919d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
920d560168bSGuenter Roeck 
921d560168bSGuenter Roeck /**
922e5d21072SGuenter Roeck  * hwmon_device_register_for_thermal - register hwmon device for thermal subsystem
923e5d21072SGuenter Roeck  * @dev: the parent device
924e5d21072SGuenter Roeck  * @name: hwmon name attribute
925e5d21072SGuenter Roeck  * @drvdata: driver data to attach to created device
926e5d21072SGuenter Roeck  *
927e5d21072SGuenter Roeck  * The use of this function is restricted. It is provided for legacy reasons
928e5d21072SGuenter Roeck  * and must only be called from the thermal subsystem.
929e5d21072SGuenter Roeck  *
930e5d21072SGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
931e5d21072SGuenter Roeck  * longer needed.
932e5d21072SGuenter Roeck  *
933e5d21072SGuenter Roeck  * Returns the pointer to the new device.
934e5d21072SGuenter Roeck  */
935e5d21072SGuenter Roeck struct device *
936e5d21072SGuenter Roeck hwmon_device_register_for_thermal(struct device *dev, const char *name,
937e5d21072SGuenter Roeck 				  void *drvdata)
938e5d21072SGuenter Roeck {
939e5d21072SGuenter Roeck 	if (!name || !dev)
940e5d21072SGuenter Roeck 		return ERR_PTR(-EINVAL);
941e5d21072SGuenter Roeck 
942e5d21072SGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, NULL, NULL);
943e5d21072SGuenter Roeck }
944e5d21072SGuenter Roeck EXPORT_SYMBOL_NS_GPL(hwmon_device_register_for_thermal, HWMON_THERMAL);
945e5d21072SGuenter Roeck 
946e5d21072SGuenter Roeck /**
9471beeffe4STony Jones  * hwmon_device_register - register w/ hwmon
9481236441fSMark M. Hoffman  * @dev: the device to register
9491236441fSMark M. Hoffman  *
9501beeffe4STony Jones  * hwmon_device_unregister() must be called when the device is no
9511236441fSMark M. Hoffman  * longer needed.
9521236441fSMark M. Hoffman  *
9531beeffe4STony Jones  * Returns the pointer to the new device.
9541236441fSMark M. Hoffman  */
9551beeffe4STony Jones struct device *hwmon_device_register(struct device *dev)
9561236441fSMark M. Hoffman {
957af1bd36cSGuenter Roeck 	dev_warn(dev,
958af1bd36cSGuenter Roeck 		 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
959af1bd36cSGuenter Roeck 
9608353863aSGuenter Roeck 	return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
9611236441fSMark M. Hoffman }
962839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register);
9631236441fSMark M. Hoffman 
9641236441fSMark M. Hoffman /**
9651236441fSMark M. Hoffman  * hwmon_device_unregister - removes the previously registered class device
9661236441fSMark M. Hoffman  *
9671beeffe4STony Jones  * @dev: the class device to destroy
9681236441fSMark M. Hoffman  */
9691beeffe4STony Jones void hwmon_device_unregister(struct device *dev)
9701236441fSMark M. Hoffman {
9711236441fSMark M. Hoffman 	int id;
9721236441fSMark M. Hoffman 
973739cf3a2SKay Sievers 	if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
9741beeffe4STony Jones 		device_unregister(dev);
975718fbfa5Skeliu 		ida_free(&hwmon_ida, id);
9761236441fSMark M. Hoffman 	} else
9771beeffe4STony Jones 		dev_dbg(dev->parent,
9781236441fSMark M. Hoffman 			"hwmon_device_unregister() failed: bad class ID!\n");
9791236441fSMark M. Hoffman }
980839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister);
9811236441fSMark M. Hoffman 
98274188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res)
98374188cbaSGuenter Roeck {
98474188cbaSGuenter Roeck 	struct device *hwdev = *(struct device **)res;
98574188cbaSGuenter Roeck 
98674188cbaSGuenter Roeck 	hwmon_device_unregister(hwdev);
98774188cbaSGuenter Roeck }
98874188cbaSGuenter Roeck 
98974188cbaSGuenter Roeck /**
99074188cbaSGuenter Roeck  * devm_hwmon_device_register_with_groups - register w/ hwmon
99174188cbaSGuenter Roeck  * @dev: the parent device
99274188cbaSGuenter Roeck  * @name: hwmon name attribute
99374188cbaSGuenter Roeck  * @drvdata: driver data to attach to created device
99474188cbaSGuenter Roeck  * @groups: List of attribute groups to create
99574188cbaSGuenter Roeck  *
99674188cbaSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
99774188cbaSGuenter Roeck  * unregistered with the parent device.
99874188cbaSGuenter Roeck  */
99974188cbaSGuenter Roeck struct device *
100074188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
100174188cbaSGuenter Roeck 				       void *drvdata,
100274188cbaSGuenter Roeck 				       const struct attribute_group **groups)
100374188cbaSGuenter Roeck {
100474188cbaSGuenter Roeck 	struct device **ptr, *hwdev;
100574188cbaSGuenter Roeck 
100674188cbaSGuenter Roeck 	if (!dev)
100774188cbaSGuenter Roeck 		return ERR_PTR(-EINVAL);
100874188cbaSGuenter Roeck 
100974188cbaSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
101074188cbaSGuenter Roeck 	if (!ptr)
101174188cbaSGuenter Roeck 		return ERR_PTR(-ENOMEM);
101274188cbaSGuenter Roeck 
101374188cbaSGuenter Roeck 	hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
101474188cbaSGuenter Roeck 	if (IS_ERR(hwdev))
101574188cbaSGuenter Roeck 		goto error;
101674188cbaSGuenter Roeck 
101774188cbaSGuenter Roeck 	*ptr = hwdev;
101874188cbaSGuenter Roeck 	devres_add(dev, ptr);
101974188cbaSGuenter Roeck 	return hwdev;
102074188cbaSGuenter Roeck 
102174188cbaSGuenter Roeck error:
102274188cbaSGuenter Roeck 	devres_free(ptr);
102374188cbaSGuenter Roeck 	return hwdev;
102474188cbaSGuenter Roeck }
102574188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
102674188cbaSGuenter Roeck 
1027d560168bSGuenter Roeck /**
1028d560168bSGuenter Roeck  * devm_hwmon_device_register_with_info - register w/ hwmon
1029d560168bSGuenter Roeck  * @dev:	the parent device
1030d560168bSGuenter Roeck  * @name:	hwmon name attribute
1031d560168bSGuenter Roeck  * @drvdata:	driver data to attach to created device
10323870945aSGuenter Roeck  * @chip:	pointer to hwmon chip information
10333870945aSGuenter Roeck  * @groups:	pointer to list of driver specific attribute groups
1034d560168bSGuenter Roeck  *
1035d560168bSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
1036d560168bSGuenter Roeck  * unregistered with the parent device.
1037d560168bSGuenter Roeck  */
1038d560168bSGuenter Roeck struct device *
1039d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name,
1040d560168bSGuenter Roeck 				     void *drvdata,
1041d560168bSGuenter Roeck 				     const struct hwmon_chip_info *chip,
1042d560168bSGuenter Roeck 				     const struct attribute_group **groups)
1043d560168bSGuenter Roeck {
1044d560168bSGuenter Roeck 	struct device **ptr, *hwdev;
1045d560168bSGuenter Roeck 
1046d560168bSGuenter Roeck 	if (!dev)
1047d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
1048d560168bSGuenter Roeck 
1049d560168bSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
1050d560168bSGuenter Roeck 	if (!ptr)
1051d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
1052d560168bSGuenter Roeck 
1053d560168bSGuenter Roeck 	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
1054d560168bSGuenter Roeck 						groups);
1055d560168bSGuenter Roeck 	if (IS_ERR(hwdev))
1056d560168bSGuenter Roeck 		goto error;
1057d560168bSGuenter Roeck 
1058d560168bSGuenter Roeck 	*ptr = hwdev;
1059d560168bSGuenter Roeck 	devres_add(dev, ptr);
1060d560168bSGuenter Roeck 
1061d560168bSGuenter Roeck 	return hwdev;
1062d560168bSGuenter Roeck 
1063d560168bSGuenter Roeck error:
1064d560168bSGuenter Roeck 	devres_free(ptr);
1065d560168bSGuenter Roeck 	return hwdev;
1066d560168bSGuenter Roeck }
1067d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
1068d560168bSGuenter Roeck 
106974188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data)
107074188cbaSGuenter Roeck {
107174188cbaSGuenter Roeck 	struct device **hwdev = res;
107274188cbaSGuenter Roeck 
107374188cbaSGuenter Roeck 	return *hwdev == data;
107474188cbaSGuenter Roeck }
107574188cbaSGuenter Roeck 
107674188cbaSGuenter Roeck /**
107774188cbaSGuenter Roeck  * devm_hwmon_device_unregister - removes a previously registered hwmon device
107874188cbaSGuenter Roeck  *
107974188cbaSGuenter Roeck  * @dev: the parent device of the device to unregister
108074188cbaSGuenter Roeck  */
108174188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev)
108274188cbaSGuenter Roeck {
108374188cbaSGuenter Roeck 	WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
108474188cbaSGuenter Roeck }
108574188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
108674188cbaSGuenter Roeck 
10871ad6c3b7SMichael Walle static char *__hwmon_sanitize_name(struct device *dev, const char *old_name)
10881ad6c3b7SMichael Walle {
10891ad6c3b7SMichael Walle 	char *name, *p;
10901ad6c3b7SMichael Walle 
10911ad6c3b7SMichael Walle 	if (dev)
10921ad6c3b7SMichael Walle 		name = devm_kstrdup(dev, old_name, GFP_KERNEL);
10931ad6c3b7SMichael Walle 	else
10941ad6c3b7SMichael Walle 		name = kstrdup(old_name, GFP_KERNEL);
10951ad6c3b7SMichael Walle 	if (!name)
10961ad6c3b7SMichael Walle 		return ERR_PTR(-ENOMEM);
10971ad6c3b7SMichael Walle 
10981ad6c3b7SMichael Walle 	for (p = name; *p; p++)
10991ad6c3b7SMichael Walle 		if (hwmon_is_bad_char(*p))
11001ad6c3b7SMichael Walle 			*p = '_';
11011ad6c3b7SMichael Walle 
11021ad6c3b7SMichael Walle 	return name;
11031ad6c3b7SMichael Walle }
11041ad6c3b7SMichael Walle 
11051ad6c3b7SMichael Walle /**
11061ad6c3b7SMichael Walle  * hwmon_sanitize_name - Replaces invalid characters in a hwmon name
11071ad6c3b7SMichael Walle  * @name: NUL-terminated name
11081ad6c3b7SMichael Walle  *
11091ad6c3b7SMichael Walle  * Allocates a new string where any invalid characters will be replaced
11101ad6c3b7SMichael Walle  * by an underscore. It is the responsibility of the caller to release
11111ad6c3b7SMichael Walle  * the memory.
11121ad6c3b7SMichael Walle  *
11131ad6c3b7SMichael Walle  * Returns newly allocated name, or ERR_PTR on error.
11141ad6c3b7SMichael Walle  */
11151ad6c3b7SMichael Walle char *hwmon_sanitize_name(const char *name)
11161ad6c3b7SMichael Walle {
11171ad6c3b7SMichael Walle 	return __hwmon_sanitize_name(NULL, name);
11181ad6c3b7SMichael Walle }
11191ad6c3b7SMichael Walle EXPORT_SYMBOL_GPL(hwmon_sanitize_name);
11201ad6c3b7SMichael Walle 
11211ad6c3b7SMichael Walle /**
11221ad6c3b7SMichael Walle  * devm_hwmon_sanitize_name - resource managed hwmon_sanitize_name()
11231ad6c3b7SMichael Walle  * @dev: device to allocate memory for
11241ad6c3b7SMichael Walle  * @name: NUL-terminated name
11251ad6c3b7SMichael Walle  *
11261ad6c3b7SMichael Walle  * Allocates a new string where any invalid characters will be replaced
11271ad6c3b7SMichael Walle  * by an underscore.
11281ad6c3b7SMichael Walle  *
11291ad6c3b7SMichael Walle  * Returns newly allocated name, or ERR_PTR on error.
11301ad6c3b7SMichael Walle  */
11311ad6c3b7SMichael Walle char *devm_hwmon_sanitize_name(struct device *dev, const char *name)
11321ad6c3b7SMichael Walle {
11331ad6c3b7SMichael Walle 	if (!dev)
11341ad6c3b7SMichael Walle 		return ERR_PTR(-EINVAL);
11351ad6c3b7SMichael Walle 
11361ad6c3b7SMichael Walle 	return __hwmon_sanitize_name(dev, name);
11371ad6c3b7SMichael Walle }
11381ad6c3b7SMichael Walle EXPORT_SYMBOL_GPL(devm_hwmon_sanitize_name);
11391ad6c3b7SMichael Walle 
11402958b1ecSJean Delvare static void __init hwmon_pci_quirks(void)
11412958b1ecSJean Delvare {
11422958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI
11432958b1ecSJean Delvare 	struct pci_dev *sb;
11442958b1ecSJean Delvare 	u16 base;
11452958b1ecSJean Delvare 	u8 enable;
11462958b1ecSJean Delvare 
11472958b1ecSJean Delvare 	/* Open access to 0x295-0x296 on MSI MS-7031 */
11482958b1ecSJean Delvare 	sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
1149d6dab7ddSJean Delvare 	if (sb) {
1150d6dab7ddSJean Delvare 		if (sb->subsystem_vendor == 0x1462 &&	/* MSI */
1151d6dab7ddSJean Delvare 		    sb->subsystem_device == 0x0031) {	/* MS-7031 */
11522958b1ecSJean Delvare 			pci_read_config_byte(sb, 0x48, &enable);
11532958b1ecSJean Delvare 			pci_read_config_word(sb, 0x64, &base);
11542958b1ecSJean Delvare 
11552958b1ecSJean Delvare 			if (base == 0 && !(enable & BIT(2))) {
11562958b1ecSJean Delvare 				dev_info(&sb->dev,
11572958b1ecSJean Delvare 					 "Opening wide generic port at 0x295\n");
11582958b1ecSJean Delvare 				pci_write_config_word(sb, 0x64, 0x295);
1159d6dab7ddSJean Delvare 				pci_write_config_byte(sb, 0x48,
1160d6dab7ddSJean Delvare 						      enable | BIT(2));
11612958b1ecSJean Delvare 			}
11622958b1ecSJean Delvare 		}
1163d6dab7ddSJean Delvare 		pci_dev_put(sb);
1164d6dab7ddSJean Delvare 	}
11652958b1ecSJean Delvare #endif
11662958b1ecSJean Delvare }
11672958b1ecSJean Delvare 
11681236441fSMark M. Hoffman static int __init hwmon_init(void)
11691236441fSMark M. Hoffman {
1170bab2243cSGuenter Roeck 	int err;
1171bab2243cSGuenter Roeck 
11722958b1ecSJean Delvare 	hwmon_pci_quirks();
11732958b1ecSJean Delvare 
1174bab2243cSGuenter Roeck 	err = class_register(&hwmon_class);
1175bab2243cSGuenter Roeck 	if (err) {
1176bab2243cSGuenter Roeck 		pr_err("couldn't register hwmon sysfs class\n");
1177bab2243cSGuenter Roeck 		return err;
11781236441fSMark M. Hoffman 	}
11791236441fSMark M. Hoffman 	return 0;
11801236441fSMark M. Hoffman }
11811236441fSMark M. Hoffman 
11821236441fSMark M. Hoffman static void __exit hwmon_exit(void)
11831236441fSMark M. Hoffman {
1184bab2243cSGuenter Roeck 	class_unregister(&hwmon_class);
11851236441fSMark M. Hoffman }
11861236441fSMark M. Hoffman 
118737f54ee5SDavid Brownell subsys_initcall(hwmon_init);
11881236441fSMark M. Hoffman module_exit(hwmon_exit);
11891236441fSMark M. Hoffman 
11901236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
11911236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
11921236441fSMark M. Hoffman MODULE_LICENSE("GPL");
11931236441fSMark M. Hoffman 
1194