xref: /openbmc/linux/drivers/hwmon/hwmon.c (revision e1c9d6d61ddf3f34f14d3de51d6eea68683b5841)
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>
181597b374SGuenter Roeck #include <linux/list.h>
19c9ebbe6fSGuenter Roeck #include <linux/module.h>
202958b1ecSJean Delvare #include <linux/pci.h>
21*e1c9d6d6SPaul Cercueil #include <linux/property.h>
22c9ebbe6fSGuenter Roeck #include <linux/slab.h>
23648cd48cSGuenter Roeck #include <linux/string.h>
24d560168bSGuenter Roeck #include <linux/thermal.h>
251236441fSMark M. Hoffman 
2661b8ab2cSNicolin Chen #define CREATE_TRACE_POINTS
2761b8ab2cSNicolin Chen #include <trace/events/hwmon.h>
2861b8ab2cSNicolin Chen 
291236441fSMark M. Hoffman #define HWMON_ID_PREFIX "hwmon"
301236441fSMark M. Hoffman #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
311236441fSMark M. Hoffman 
32bab2243cSGuenter Roeck struct hwmon_device {
33bab2243cSGuenter Roeck 	const char *name;
34*e1c9d6d6SPaul Cercueil 	const char *label;
35bab2243cSGuenter Roeck 	struct device dev;
36d560168bSGuenter Roeck 	const struct hwmon_chip_info *chip;
371597b374SGuenter Roeck 	struct list_head tzdata;
38d560168bSGuenter Roeck 	struct attribute_group group;
39d560168bSGuenter Roeck 	const struct attribute_group **groups;
40bab2243cSGuenter Roeck };
41d560168bSGuenter Roeck 
42bab2243cSGuenter Roeck #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
43bab2243cSGuenter Roeck 
443a412d5eSGuenter Roeck #define MAX_SYSFS_ATTR_NAME_LENGTH	32
453a412d5eSGuenter Roeck 
46d560168bSGuenter Roeck struct hwmon_device_attribute {
47d560168bSGuenter Roeck 	struct device_attribute dev_attr;
48d560168bSGuenter Roeck 	const struct hwmon_ops *ops;
49d560168bSGuenter Roeck 	enum hwmon_sensor_types type;
50d560168bSGuenter Roeck 	u32 attr;
51d560168bSGuenter Roeck 	int index;
523a412d5eSGuenter Roeck 	char name[MAX_SYSFS_ATTR_NAME_LENGTH];
53d560168bSGuenter Roeck };
54d560168bSGuenter Roeck 
55d560168bSGuenter Roeck #define to_hwmon_attr(d) \
56d560168bSGuenter Roeck 	container_of(d, struct hwmon_device_attribute, dev_attr)
573bf8bdcfSGuenter Roeck #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
58d560168bSGuenter Roeck 
59d560168bSGuenter Roeck /*
60d560168bSGuenter Roeck  * Thermal zone information
61d560168bSGuenter Roeck  */
62d560168bSGuenter Roeck struct hwmon_thermal_data {
631597b374SGuenter Roeck 	struct list_head node;		/* hwmon tzdata list entry */
643bf8bdcfSGuenter Roeck 	struct device *dev;		/* Reference to hwmon device */
65d560168bSGuenter Roeck 	int index;			/* sensor index */
661597b374SGuenter Roeck 	struct thermal_zone_device *tzd;/* thermal zone device */
67d560168bSGuenter Roeck };
68d560168bSGuenter Roeck 
69bab2243cSGuenter Roeck static ssize_t
702ab0c6c5SJulia Lawall name_show(struct device *dev, struct device_attribute *attr, char *buf)
71bab2243cSGuenter Roeck {
72bab2243cSGuenter Roeck 	return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
73bab2243cSGuenter Roeck }
742ab0c6c5SJulia Lawall static DEVICE_ATTR_RO(name);
75bab2243cSGuenter Roeck 
76*e1c9d6d6SPaul Cercueil static ssize_t
77*e1c9d6d6SPaul Cercueil label_show(struct device *dev, struct device_attribute *attr, char *buf)
78*e1c9d6d6SPaul Cercueil {
79*e1c9d6d6SPaul Cercueil 	return sysfs_emit(buf, "%s\n", to_hwmon_device(dev)->label);
80*e1c9d6d6SPaul Cercueil }
81*e1c9d6d6SPaul Cercueil static DEVICE_ATTR_RO(label);
82*e1c9d6d6SPaul Cercueil 
83bab2243cSGuenter Roeck static struct attribute *hwmon_dev_attrs[] = {
84bab2243cSGuenter Roeck 	&dev_attr_name.attr,
85*e1c9d6d6SPaul Cercueil 	&dev_attr_label.attr,
86bab2243cSGuenter Roeck 	NULL
87bab2243cSGuenter Roeck };
88bab2243cSGuenter Roeck 
89*e1c9d6d6SPaul Cercueil static umode_t hwmon_dev_attr_is_visible(struct kobject *kobj,
90bab2243cSGuenter Roeck 					 struct attribute *attr, int n)
91bab2243cSGuenter Roeck {
9277d76768SYang Li 	struct device *dev = kobj_to_dev(kobj);
93*e1c9d6d6SPaul Cercueil 	struct hwmon_device *hdev = to_hwmon_device(dev);
94bab2243cSGuenter Roeck 
95*e1c9d6d6SPaul Cercueil 	if (attr == &dev_attr_name.attr && hdev->name == NULL)
96*e1c9d6d6SPaul Cercueil 		return 0;
97*e1c9d6d6SPaul Cercueil 
98*e1c9d6d6SPaul Cercueil 	if (attr == &dev_attr_label.attr && hdev->label == NULL)
99bab2243cSGuenter Roeck 		return 0;
100bab2243cSGuenter Roeck 
101bab2243cSGuenter Roeck 	return attr->mode;
102bab2243cSGuenter Roeck }
103bab2243cSGuenter Roeck 
104524703acSArvind Yadav static const struct attribute_group hwmon_dev_attr_group = {
105bab2243cSGuenter Roeck 	.attrs		= hwmon_dev_attrs,
106*e1c9d6d6SPaul Cercueil 	.is_visible	= hwmon_dev_attr_is_visible,
107bab2243cSGuenter Roeck };
108bab2243cSGuenter Roeck 
109bab2243cSGuenter Roeck static const struct attribute_group *hwmon_dev_attr_groups[] = {
110bab2243cSGuenter Roeck 	&hwmon_dev_attr_group,
111bab2243cSGuenter Roeck 	NULL
112bab2243cSGuenter Roeck };
113bab2243cSGuenter Roeck 
1143bf8bdcfSGuenter Roeck static void hwmon_free_attrs(struct attribute **attrs)
1153bf8bdcfSGuenter Roeck {
1163bf8bdcfSGuenter Roeck 	int i;
1173bf8bdcfSGuenter Roeck 
1183bf8bdcfSGuenter Roeck 	for (i = 0; attrs[i]; i++) {
1193bf8bdcfSGuenter Roeck 		struct device_attribute *dattr = to_dev_attr(attrs[i]);
1203bf8bdcfSGuenter Roeck 		struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr);
1213bf8bdcfSGuenter Roeck 
1223bf8bdcfSGuenter Roeck 		kfree(hattr);
1233bf8bdcfSGuenter Roeck 	}
1243bf8bdcfSGuenter Roeck 	kfree(attrs);
1253bf8bdcfSGuenter Roeck }
1263bf8bdcfSGuenter Roeck 
127bab2243cSGuenter Roeck static void hwmon_dev_release(struct device *dev)
128bab2243cSGuenter Roeck {
1293bf8bdcfSGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(dev);
1303bf8bdcfSGuenter Roeck 
1313bf8bdcfSGuenter Roeck 	if (hwdev->group.attrs)
1323bf8bdcfSGuenter Roeck 		hwmon_free_attrs(hwdev->group.attrs);
1333bf8bdcfSGuenter Roeck 	kfree(hwdev->groups);
134*e1c9d6d6SPaul Cercueil 	kfree(hwdev->label);
1353bf8bdcfSGuenter Roeck 	kfree(hwdev);
136bab2243cSGuenter Roeck }
137bab2243cSGuenter Roeck 
138bab2243cSGuenter Roeck static struct class hwmon_class = {
139bab2243cSGuenter Roeck 	.name = "hwmon",
140bab2243cSGuenter Roeck 	.owner = THIS_MODULE,
141bab2243cSGuenter Roeck 	.dev_groups = hwmon_dev_attr_groups,
142bab2243cSGuenter Roeck 	.dev_release = hwmon_dev_release,
143bab2243cSGuenter Roeck };
1441236441fSMark M. Hoffman 
1454ca5f468SJonathan Cameron static DEFINE_IDA(hwmon_ida);
1461236441fSMark M. Hoffman 
147d560168bSGuenter Roeck /* Thermal zone handling */
148d560168bSGuenter Roeck 
14986430c1aSGuenter Roeck /*
15086430c1aSGuenter Roeck  * The complex conditional is necessary to avoid a cyclic dependency
15186430c1aSGuenter Roeck  * between hwmon and thermal_sys modules.
15286430c1aSGuenter Roeck  */
153f3735332SDaniel Lezcano #ifdef CONFIG_THERMAL_OF
154d560168bSGuenter Roeck static int hwmon_thermal_get_temp(void *data, int *temp)
155d560168bSGuenter Roeck {
156d560168bSGuenter Roeck 	struct hwmon_thermal_data *tdata = data;
1573bf8bdcfSGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
158d560168bSGuenter Roeck 	int ret;
159d560168bSGuenter Roeck 	long t;
160d560168bSGuenter Roeck 
1613bf8bdcfSGuenter Roeck 	ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
162d560168bSGuenter Roeck 				     tdata->index, &t);
163d560168bSGuenter Roeck 	if (ret < 0)
164d560168bSGuenter Roeck 		return ret;
165d560168bSGuenter Roeck 
166d560168bSGuenter Roeck 	*temp = t;
167d560168bSGuenter Roeck 
168d560168bSGuenter Roeck 	return 0;
169d560168bSGuenter Roeck }
170d560168bSGuenter Roeck 
171a5f6c0f8SDmitry Osipenko static int hwmon_thermal_set_trips(void *data, int low, int high)
172a5f6c0f8SDmitry Osipenko {
173a5f6c0f8SDmitry Osipenko 	struct hwmon_thermal_data *tdata = data;
174a5f6c0f8SDmitry Osipenko 	struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
175a5f6c0f8SDmitry Osipenko 	const struct hwmon_chip_info *chip = hwdev->chip;
176a5f6c0f8SDmitry Osipenko 	const struct hwmon_channel_info **info = chip->info;
177a5f6c0f8SDmitry Osipenko 	unsigned int i;
178a5f6c0f8SDmitry Osipenko 	int err;
179a5f6c0f8SDmitry Osipenko 
180a5f6c0f8SDmitry Osipenko 	if (!chip->ops->write)
181a5f6c0f8SDmitry Osipenko 		return 0;
182a5f6c0f8SDmitry Osipenko 
183a5f6c0f8SDmitry Osipenko 	for (i = 0; info[i] && info[i]->type != hwmon_temp; i++)
184a5f6c0f8SDmitry Osipenko 		continue;
185a5f6c0f8SDmitry Osipenko 
186a5f6c0f8SDmitry Osipenko 	if (!info[i])
187a5f6c0f8SDmitry Osipenko 		return 0;
188a5f6c0f8SDmitry Osipenko 
189a5f6c0f8SDmitry Osipenko 	if (info[i]->config[tdata->index] & HWMON_T_MIN) {
190a5f6c0f8SDmitry Osipenko 		err = chip->ops->write(tdata->dev, hwmon_temp,
191a5f6c0f8SDmitry Osipenko 				       hwmon_temp_min, tdata->index, low);
192a5f6c0f8SDmitry Osipenko 		if (err && err != -EOPNOTSUPP)
193a5f6c0f8SDmitry Osipenko 			return err;
194a5f6c0f8SDmitry Osipenko 	}
195a5f6c0f8SDmitry Osipenko 
196a5f6c0f8SDmitry Osipenko 	if (info[i]->config[tdata->index] & HWMON_T_MAX) {
197a5f6c0f8SDmitry Osipenko 		err = chip->ops->write(tdata->dev, hwmon_temp,
198a5f6c0f8SDmitry Osipenko 				       hwmon_temp_max, tdata->index, high);
199a5f6c0f8SDmitry Osipenko 		if (err && err != -EOPNOTSUPP)
200a5f6c0f8SDmitry Osipenko 			return err;
201a5f6c0f8SDmitry Osipenko 	}
202a5f6c0f8SDmitry Osipenko 
203a5f6c0f8SDmitry Osipenko 	return 0;
204a5f6c0f8SDmitry Osipenko }
205a5f6c0f8SDmitry Osipenko 
206c9920650SJulia Lawall static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
207d560168bSGuenter Roeck 	.get_temp = hwmon_thermal_get_temp,
208a5f6c0f8SDmitry Osipenko 	.set_trips = hwmon_thermal_set_trips,
209d560168bSGuenter Roeck };
210d560168bSGuenter Roeck 
2111597b374SGuenter Roeck static void hwmon_thermal_remove_sensor(void *data)
2121597b374SGuenter Roeck {
2131597b374SGuenter Roeck 	list_del(data);
2141597b374SGuenter Roeck }
2151597b374SGuenter Roeck 
2163bf8bdcfSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, int index)
217d560168bSGuenter Roeck {
2181597b374SGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(dev);
219d560168bSGuenter Roeck 	struct hwmon_thermal_data *tdata;
22047c332deSLinus Walleij 	struct thermal_zone_device *tzd;
2211597b374SGuenter Roeck 	int err;
222d560168bSGuenter Roeck 
223d560168bSGuenter Roeck 	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
224d560168bSGuenter Roeck 	if (!tdata)
225d560168bSGuenter Roeck 		return -ENOMEM;
226d560168bSGuenter Roeck 
2273bf8bdcfSGuenter Roeck 	tdata->dev = dev;
228d560168bSGuenter Roeck 	tdata->index = index;
229d560168bSGuenter Roeck 
2303bf8bdcfSGuenter Roeck 	tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata,
231d560168bSGuenter Roeck 						   &hwmon_thermal_ops);
2321b5f517cSGuenter Roeck 	if (IS_ERR(tzd)) {
2331b5f517cSGuenter Roeck 		if (PTR_ERR(tzd) != -ENODEV)
23447c332deSLinus Walleij 			return PTR_ERR(tzd);
2351b5f517cSGuenter Roeck 		dev_info(dev, "temp%d_input not attached to any thermal zone\n",
2361b5f517cSGuenter Roeck 			 index + 1);
2371b5f517cSGuenter Roeck 		devm_kfree(dev, tdata);
2381b5f517cSGuenter Roeck 		return 0;
2391b5f517cSGuenter Roeck 	}
240d560168bSGuenter Roeck 
2411597b374SGuenter Roeck 	err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node);
2421597b374SGuenter Roeck 	if (err)
2431597b374SGuenter Roeck 		return err;
2441597b374SGuenter Roeck 
2451597b374SGuenter Roeck 	tdata->tzd = tzd;
2461597b374SGuenter Roeck 	list_add(&tdata->node, &hwdev->tzdata);
2471597b374SGuenter Roeck 
248d560168bSGuenter Roeck 	return 0;
249d560168bSGuenter Roeck }
25044e3ad88SAkinobu Mita 
25144e3ad88SAkinobu Mita static int hwmon_thermal_register_sensors(struct device *dev)
25244e3ad88SAkinobu Mita {
25344e3ad88SAkinobu Mita 	struct hwmon_device *hwdev = to_hwmon_device(dev);
25444e3ad88SAkinobu Mita 	const struct hwmon_chip_info *chip = hwdev->chip;
25544e3ad88SAkinobu Mita 	const struct hwmon_channel_info **info = chip->info;
25644e3ad88SAkinobu Mita 	void *drvdata = dev_get_drvdata(dev);
25744e3ad88SAkinobu Mita 	int i;
25844e3ad88SAkinobu Mita 
25944e3ad88SAkinobu Mita 	for (i = 1; info[i]; i++) {
26044e3ad88SAkinobu Mita 		int j;
26144e3ad88SAkinobu Mita 
26244e3ad88SAkinobu Mita 		if (info[i]->type != hwmon_temp)
26344e3ad88SAkinobu Mita 			continue;
26444e3ad88SAkinobu Mita 
26544e3ad88SAkinobu Mita 		for (j = 0; info[i]->config[j]; j++) {
26644e3ad88SAkinobu Mita 			int err;
26744e3ad88SAkinobu Mita 
26844e3ad88SAkinobu Mita 			if (!(info[i]->config[j] & HWMON_T_INPUT) ||
26944e3ad88SAkinobu Mita 			    !chip->ops->is_visible(drvdata, hwmon_temp,
27044e3ad88SAkinobu Mita 						   hwmon_temp_input, j))
27144e3ad88SAkinobu Mita 				continue;
27244e3ad88SAkinobu Mita 
27344e3ad88SAkinobu Mita 			err = hwmon_thermal_add_sensor(dev, j);
27444e3ad88SAkinobu Mita 			if (err)
27544e3ad88SAkinobu Mita 				return err;
27644e3ad88SAkinobu Mita 		}
27744e3ad88SAkinobu Mita 	}
27844e3ad88SAkinobu Mita 
27944e3ad88SAkinobu Mita 	return 0;
28044e3ad88SAkinobu Mita }
28144e3ad88SAkinobu Mita 
2821597b374SGuenter Roeck static void hwmon_thermal_notify(struct device *dev, int index)
2831597b374SGuenter Roeck {
2841597b374SGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(dev);
2851597b374SGuenter Roeck 	struct hwmon_thermal_data *tzdata;
2861597b374SGuenter Roeck 
2871597b374SGuenter Roeck 	list_for_each_entry(tzdata, &hwdev->tzdata, node) {
2881597b374SGuenter Roeck 		if (tzdata->index == index) {
2891597b374SGuenter Roeck 			thermal_zone_device_update(tzdata->tzd,
2901597b374SGuenter Roeck 						   THERMAL_EVENT_UNSPECIFIED);
2911597b374SGuenter Roeck 		}
2921597b374SGuenter Roeck 	}
2931597b374SGuenter Roeck }
2941597b374SGuenter Roeck 
295d560168bSGuenter Roeck #else
29644e3ad88SAkinobu Mita static int hwmon_thermal_register_sensors(struct device *dev)
297d560168bSGuenter Roeck {
298d560168bSGuenter Roeck 	return 0;
299d560168bSGuenter Roeck }
3001597b374SGuenter Roeck 
3011597b374SGuenter Roeck static void hwmon_thermal_notify(struct device *dev, int index) { }
3021597b374SGuenter Roeck 
30386430c1aSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
304d560168bSGuenter Roeck 
30561b8ab2cSNicolin Chen static int hwmon_attr_base(enum hwmon_sensor_types type)
30661b8ab2cSNicolin Chen {
3074413405fSDr. David Alan Gilbert 	if (type == hwmon_in || type == hwmon_intrusion)
30861b8ab2cSNicolin Chen 		return 0;
30961b8ab2cSNicolin Chen 	return 1;
31061b8ab2cSNicolin Chen }
31161b8ab2cSNicolin Chen 
312d560168bSGuenter Roeck /* sysfs attribute management */
313d560168bSGuenter Roeck 
314d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev,
315d560168bSGuenter Roeck 			       struct device_attribute *devattr, char *buf)
316d560168bSGuenter Roeck {
317d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
318d560168bSGuenter Roeck 	long val;
319d560168bSGuenter Roeck 	int ret;
320d560168bSGuenter Roeck 
321d560168bSGuenter Roeck 	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
322d560168bSGuenter Roeck 			       &val);
323d560168bSGuenter Roeck 	if (ret < 0)
324d560168bSGuenter Roeck 		return ret;
325d560168bSGuenter Roeck 
32661b8ab2cSNicolin Chen 	trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
32761b8ab2cSNicolin Chen 			      hattr->name, val);
32861b8ab2cSNicolin Chen 
329d560168bSGuenter Roeck 	return sprintf(buf, "%ld\n", val);
330d560168bSGuenter Roeck }
331d560168bSGuenter Roeck 
332e159ab5cSGuenter Roeck static ssize_t hwmon_attr_show_string(struct device *dev,
333e159ab5cSGuenter Roeck 				      struct device_attribute *devattr,
334e159ab5cSGuenter Roeck 				      char *buf)
335e159ab5cSGuenter Roeck {
336e159ab5cSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
33761b8ab2cSNicolin Chen 	enum hwmon_sensor_types type = hattr->type;
3385ba6bcbcSJean Delvare 	const char *s;
339e159ab5cSGuenter Roeck 	int ret;
340e159ab5cSGuenter Roeck 
341e159ab5cSGuenter Roeck 	ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
342e159ab5cSGuenter Roeck 				      hattr->index, &s);
343e159ab5cSGuenter Roeck 	if (ret < 0)
344e159ab5cSGuenter Roeck 		return ret;
345e159ab5cSGuenter Roeck 
34661b8ab2cSNicolin Chen 	trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
34761b8ab2cSNicolin Chen 				     hattr->name, s);
34861b8ab2cSNicolin Chen 
349e159ab5cSGuenter Roeck 	return sprintf(buf, "%s\n", s);
350e159ab5cSGuenter Roeck }
351e159ab5cSGuenter Roeck 
352d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev,
353d560168bSGuenter Roeck 				struct device_attribute *devattr,
354d560168bSGuenter Roeck 				const char *buf, size_t count)
355d560168bSGuenter Roeck {
356d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
357d560168bSGuenter Roeck 	long val;
358d560168bSGuenter Roeck 	int ret;
359d560168bSGuenter Roeck 
360d560168bSGuenter Roeck 	ret = kstrtol(buf, 10, &val);
361d560168bSGuenter Roeck 	if (ret < 0)
362d560168bSGuenter Roeck 		return ret;
363d560168bSGuenter Roeck 
364d560168bSGuenter Roeck 	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
365d560168bSGuenter Roeck 				val);
366d560168bSGuenter Roeck 	if (ret < 0)
367d560168bSGuenter Roeck 		return ret;
368d560168bSGuenter Roeck 
36961b8ab2cSNicolin Chen 	trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
37061b8ab2cSNicolin Chen 			       hattr->name, val);
371d560168bSGuenter Roeck 
37261b8ab2cSNicolin Chen 	return count;
373d560168bSGuenter Roeck }
374d560168bSGuenter Roeck 
375e159ab5cSGuenter Roeck static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
376e159ab5cSGuenter Roeck {
377e159ab5cSGuenter Roeck 	return (type == hwmon_temp && attr == hwmon_temp_label) ||
378e159ab5cSGuenter Roeck 	       (type == hwmon_in && attr == hwmon_in_label) ||
379e159ab5cSGuenter Roeck 	       (type == hwmon_curr && attr == hwmon_curr_label) ||
380e159ab5cSGuenter Roeck 	       (type == hwmon_power && attr == hwmon_power_label) ||
381e159ab5cSGuenter Roeck 	       (type == hwmon_energy && attr == hwmon_energy_label) ||
382e159ab5cSGuenter Roeck 	       (type == hwmon_humidity && attr == hwmon_humidity_label) ||
383e159ab5cSGuenter Roeck 	       (type == hwmon_fan && attr == hwmon_fan_label);
384e159ab5cSGuenter Roeck }
385e159ab5cSGuenter Roeck 
3863bf8bdcfSGuenter Roeck static struct attribute *hwmon_genattr(const void *drvdata,
387d560168bSGuenter Roeck 				       enum hwmon_sensor_types type,
388d560168bSGuenter Roeck 				       u32 attr,
389d560168bSGuenter Roeck 				       int index,
390d560168bSGuenter Roeck 				       const char *template,
391d560168bSGuenter Roeck 				       const struct hwmon_ops *ops)
392d560168bSGuenter Roeck {
393d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr;
394d560168bSGuenter Roeck 	struct device_attribute *dattr;
395d560168bSGuenter Roeck 	struct attribute *a;
396d560168bSGuenter Roeck 	umode_t mode;
3973b443defSRasmus Villemoes 	const char *name;
398e159ab5cSGuenter Roeck 	bool is_string = is_string_attr(type, attr);
399d560168bSGuenter Roeck 
400d560168bSGuenter Roeck 	/* The attribute is invisible if there is no template string */
401d560168bSGuenter Roeck 	if (!template)
402d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
403d560168bSGuenter Roeck 
404d560168bSGuenter Roeck 	mode = ops->is_visible(drvdata, type, attr, index);
405d560168bSGuenter Roeck 	if (!mode)
406d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
407d560168bSGuenter Roeck 
4080d87116fSGuenter Roeck 	if ((mode & 0444) && ((is_string && !ops->read_string) ||
409e159ab5cSGuenter Roeck 				 (!is_string && !ops->read)))
410d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
4110d87116fSGuenter Roeck 	if ((mode & 0222) && !ops->write)
412d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
413d560168bSGuenter Roeck 
4143bf8bdcfSGuenter Roeck 	hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
415d560168bSGuenter Roeck 	if (!hattr)
416d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
417d560168bSGuenter Roeck 
4183a412d5eSGuenter Roeck 	if (type == hwmon_chip) {
4193b443defSRasmus Villemoes 		name = template;
4203a412d5eSGuenter Roeck 	} else {
4213a412d5eSGuenter Roeck 		scnprintf(hattr->name, sizeof(hattr->name), template,
4223a412d5eSGuenter Roeck 			  index + hwmon_attr_base(type));
4233a412d5eSGuenter Roeck 		name = hattr->name;
4243a412d5eSGuenter Roeck 	}
4253a412d5eSGuenter Roeck 
426d560168bSGuenter Roeck 	hattr->type = type;
427d560168bSGuenter Roeck 	hattr->attr = attr;
428d560168bSGuenter Roeck 	hattr->index = index;
429d560168bSGuenter Roeck 	hattr->ops = ops;
430d560168bSGuenter Roeck 
431d560168bSGuenter Roeck 	dattr = &hattr->dev_attr;
432e159ab5cSGuenter Roeck 	dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
433d560168bSGuenter Roeck 	dattr->store = hwmon_attr_store;
434d560168bSGuenter Roeck 
435d560168bSGuenter Roeck 	a = &dattr->attr;
436d560168bSGuenter Roeck 	sysfs_attr_init(a);
437d560168bSGuenter Roeck 	a->name = name;
438d560168bSGuenter Roeck 	a->mode = mode;
439d560168bSGuenter Roeck 
440d560168bSGuenter Roeck 	return a;
441d560168bSGuenter Roeck }
442d560168bSGuenter Roeck 
443f4d325d5SGuenter Roeck /*
444f4d325d5SGuenter Roeck  * Chip attributes are not attribute templates but actual sysfs attributes.
445f4d325d5SGuenter Roeck  * See hwmon_genattr() for special handling.
446f4d325d5SGuenter Roeck  */
447f4d325d5SGuenter Roeck static const char * const hwmon_chip_attrs[] = {
448d560168bSGuenter Roeck 	[hwmon_chip_temp_reset_history] = "temp_reset_history",
44900d616cfSGuenter Roeck 	[hwmon_chip_in_reset_history] = "in_reset_history",
4509b26947cSGuenter Roeck 	[hwmon_chip_curr_reset_history] = "curr_reset_history",
451b308f5c7SGuenter Roeck 	[hwmon_chip_power_reset_history] = "power_reset_history",
452d560168bSGuenter Roeck 	[hwmon_chip_update_interval] = "update_interval",
453d560168bSGuenter Roeck 	[hwmon_chip_alarms] = "alarms",
4549f00995eSGuenter Roeck 	[hwmon_chip_samples] = "samples",
4559f00995eSGuenter Roeck 	[hwmon_chip_curr_samples] = "curr_samples",
4569f00995eSGuenter Roeck 	[hwmon_chip_in_samples] = "in_samples",
4579f00995eSGuenter Roeck 	[hwmon_chip_power_samples] = "power_samples",
4589f00995eSGuenter Roeck 	[hwmon_chip_temp_samples] = "temp_samples",
459d560168bSGuenter Roeck };
460d560168bSGuenter Roeck 
461d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = {
462002c6b54SGuenter Roeck 	[hwmon_temp_enable] = "temp%d_enable",
463d560168bSGuenter Roeck 	[hwmon_temp_input] = "temp%d_input",
464d560168bSGuenter Roeck 	[hwmon_temp_type] = "temp%d_type",
465d560168bSGuenter Roeck 	[hwmon_temp_lcrit] = "temp%d_lcrit",
466d560168bSGuenter Roeck 	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
467d560168bSGuenter Roeck 	[hwmon_temp_min] = "temp%d_min",
468d560168bSGuenter Roeck 	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
469d560168bSGuenter Roeck 	[hwmon_temp_max] = "temp%d_max",
470d560168bSGuenter Roeck 	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
471d560168bSGuenter Roeck 	[hwmon_temp_crit] = "temp%d_crit",
472d560168bSGuenter Roeck 	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
473d560168bSGuenter Roeck 	[hwmon_temp_emergency] = "temp%d_emergency",
474d560168bSGuenter Roeck 	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
475d560168bSGuenter Roeck 	[hwmon_temp_alarm] = "temp%d_alarm",
476d560168bSGuenter Roeck 	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
477d560168bSGuenter Roeck 	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
478d560168bSGuenter Roeck 	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
479d560168bSGuenter Roeck 	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
480d560168bSGuenter Roeck 	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
481d560168bSGuenter Roeck 	[hwmon_temp_fault] = "temp%d_fault",
482d560168bSGuenter Roeck 	[hwmon_temp_offset] = "temp%d_offset",
483d560168bSGuenter Roeck 	[hwmon_temp_label] = "temp%d_label",
484d560168bSGuenter Roeck 	[hwmon_temp_lowest] = "temp%d_lowest",
485d560168bSGuenter Roeck 	[hwmon_temp_highest] = "temp%d_highest",
486d560168bSGuenter Roeck 	[hwmon_temp_reset_history] = "temp%d_reset_history",
4871967f712SZbigniew Lukwinski 	[hwmon_temp_rated_min] = "temp%d_rated_min",
4881967f712SZbigniew Lukwinski 	[hwmon_temp_rated_max] = "temp%d_rated_max",
489d560168bSGuenter Roeck };
490d560168bSGuenter Roeck 
49100d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = {
492002c6b54SGuenter Roeck 	[hwmon_in_enable] = "in%d_enable",
49300d616cfSGuenter Roeck 	[hwmon_in_input] = "in%d_input",
49400d616cfSGuenter Roeck 	[hwmon_in_min] = "in%d_min",
49500d616cfSGuenter Roeck 	[hwmon_in_max] = "in%d_max",
49600d616cfSGuenter Roeck 	[hwmon_in_lcrit] = "in%d_lcrit",
49700d616cfSGuenter Roeck 	[hwmon_in_crit] = "in%d_crit",
49800d616cfSGuenter Roeck 	[hwmon_in_average] = "in%d_average",
49900d616cfSGuenter Roeck 	[hwmon_in_lowest] = "in%d_lowest",
50000d616cfSGuenter Roeck 	[hwmon_in_highest] = "in%d_highest",
50100d616cfSGuenter Roeck 	[hwmon_in_reset_history] = "in%d_reset_history",
50200d616cfSGuenter Roeck 	[hwmon_in_label] = "in%d_label",
50300d616cfSGuenter Roeck 	[hwmon_in_alarm] = "in%d_alarm",
50400d616cfSGuenter Roeck 	[hwmon_in_min_alarm] = "in%d_min_alarm",
50500d616cfSGuenter Roeck 	[hwmon_in_max_alarm] = "in%d_max_alarm",
50600d616cfSGuenter Roeck 	[hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
50700d616cfSGuenter Roeck 	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
5081967f712SZbigniew Lukwinski 	[hwmon_in_rated_min] = "in%d_rated_min",
5091967f712SZbigniew Lukwinski 	[hwmon_in_rated_max] = "in%d_rated_max",
51000d616cfSGuenter Roeck };
51100d616cfSGuenter Roeck 
5129b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = {
513002c6b54SGuenter Roeck 	[hwmon_curr_enable] = "curr%d_enable",
5149b26947cSGuenter Roeck 	[hwmon_curr_input] = "curr%d_input",
5159b26947cSGuenter Roeck 	[hwmon_curr_min] = "curr%d_min",
5169b26947cSGuenter Roeck 	[hwmon_curr_max] = "curr%d_max",
5179b26947cSGuenter Roeck 	[hwmon_curr_lcrit] = "curr%d_lcrit",
5189b26947cSGuenter Roeck 	[hwmon_curr_crit] = "curr%d_crit",
5199b26947cSGuenter Roeck 	[hwmon_curr_average] = "curr%d_average",
5209b26947cSGuenter Roeck 	[hwmon_curr_lowest] = "curr%d_lowest",
5219b26947cSGuenter Roeck 	[hwmon_curr_highest] = "curr%d_highest",
5229b26947cSGuenter Roeck 	[hwmon_curr_reset_history] = "curr%d_reset_history",
5239b26947cSGuenter Roeck 	[hwmon_curr_label] = "curr%d_label",
5249b26947cSGuenter Roeck 	[hwmon_curr_alarm] = "curr%d_alarm",
5259b26947cSGuenter Roeck 	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
5269b26947cSGuenter Roeck 	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
5279b26947cSGuenter Roeck 	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
5289b26947cSGuenter Roeck 	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
5291967f712SZbigniew Lukwinski 	[hwmon_curr_rated_min] = "curr%d_rated_min",
5301967f712SZbigniew Lukwinski 	[hwmon_curr_rated_max] = "curr%d_rated_max",
5319b26947cSGuenter Roeck };
5329b26947cSGuenter Roeck 
533b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = {
534002c6b54SGuenter Roeck 	[hwmon_power_enable] = "power%d_enable",
535b308f5c7SGuenter Roeck 	[hwmon_power_average] = "power%d_average",
536b308f5c7SGuenter Roeck 	[hwmon_power_average_interval] = "power%d_average_interval",
537b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_max] = "power%d_interval_max",
538b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_min] = "power%d_interval_min",
539b308f5c7SGuenter Roeck 	[hwmon_power_average_highest] = "power%d_average_highest",
540b308f5c7SGuenter Roeck 	[hwmon_power_average_lowest] = "power%d_average_lowest",
541b308f5c7SGuenter Roeck 	[hwmon_power_average_max] = "power%d_average_max",
542b308f5c7SGuenter Roeck 	[hwmon_power_average_min] = "power%d_average_min",
543b308f5c7SGuenter Roeck 	[hwmon_power_input] = "power%d_input",
544b308f5c7SGuenter Roeck 	[hwmon_power_input_highest] = "power%d_input_highest",
545b308f5c7SGuenter Roeck 	[hwmon_power_input_lowest] = "power%d_input_lowest",
546b308f5c7SGuenter Roeck 	[hwmon_power_reset_history] = "power%d_reset_history",
547b308f5c7SGuenter Roeck 	[hwmon_power_accuracy] = "power%d_accuracy",
548b308f5c7SGuenter Roeck 	[hwmon_power_cap] = "power%d_cap",
549b308f5c7SGuenter Roeck 	[hwmon_power_cap_hyst] = "power%d_cap_hyst",
550b308f5c7SGuenter Roeck 	[hwmon_power_cap_max] = "power%d_cap_max",
551b308f5c7SGuenter Roeck 	[hwmon_power_cap_min] = "power%d_cap_min",
552aa7f29b0SAndrew Lunn 	[hwmon_power_min] = "power%d_min",
553b308f5c7SGuenter Roeck 	[hwmon_power_max] = "power%d_max",
554aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit] = "power%d_lcrit",
555b308f5c7SGuenter Roeck 	[hwmon_power_crit] = "power%d_crit",
556b308f5c7SGuenter Roeck 	[hwmon_power_label] = "power%d_label",
557b308f5c7SGuenter Roeck 	[hwmon_power_alarm] = "power%d_alarm",
558b308f5c7SGuenter Roeck 	[hwmon_power_cap_alarm] = "power%d_cap_alarm",
559aa7f29b0SAndrew Lunn 	[hwmon_power_min_alarm] = "power%d_min_alarm",
560b308f5c7SGuenter Roeck 	[hwmon_power_max_alarm] = "power%d_max_alarm",
561aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
562b308f5c7SGuenter Roeck 	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
5631967f712SZbigniew Lukwinski 	[hwmon_power_rated_min] = "power%d_rated_min",
5641967f712SZbigniew Lukwinski 	[hwmon_power_rated_max] = "power%d_rated_max",
565b308f5c7SGuenter Roeck };
566b308f5c7SGuenter Roeck 
5676bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = {
568002c6b54SGuenter Roeck 	[hwmon_energy_enable] = "energy%d_enable",
5696bfcca44SGuenter Roeck 	[hwmon_energy_input] = "energy%d_input",
5706bfcca44SGuenter Roeck 	[hwmon_energy_label] = "energy%d_label",
5716bfcca44SGuenter Roeck };
5726bfcca44SGuenter Roeck 
5736bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = {
574002c6b54SGuenter Roeck 	[hwmon_humidity_enable] = "humidity%d_enable",
5756bfcca44SGuenter Roeck 	[hwmon_humidity_input] = "humidity%d_input",
5766bfcca44SGuenter Roeck 	[hwmon_humidity_label] = "humidity%d_label",
5776bfcca44SGuenter Roeck 	[hwmon_humidity_min] = "humidity%d_min",
5786bfcca44SGuenter Roeck 	[hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
5796bfcca44SGuenter Roeck 	[hwmon_humidity_max] = "humidity%d_max",
5806bfcca44SGuenter Roeck 	[hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
5816bfcca44SGuenter Roeck 	[hwmon_humidity_alarm] = "humidity%d_alarm",
5826bfcca44SGuenter Roeck 	[hwmon_humidity_fault] = "humidity%d_fault",
5831967f712SZbigniew Lukwinski 	[hwmon_humidity_rated_min] = "humidity%d_rated_min",
5841967f712SZbigniew Lukwinski 	[hwmon_humidity_rated_max] = "humidity%d_rated_max",
5856bfcca44SGuenter Roeck };
5866bfcca44SGuenter Roeck 
5878faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = {
588002c6b54SGuenter Roeck 	[hwmon_fan_enable] = "fan%d_enable",
5898faee73fSGuenter Roeck 	[hwmon_fan_input] = "fan%d_input",
5908faee73fSGuenter Roeck 	[hwmon_fan_label] = "fan%d_label",
5918faee73fSGuenter Roeck 	[hwmon_fan_min] = "fan%d_min",
5928faee73fSGuenter Roeck 	[hwmon_fan_max] = "fan%d_max",
5938faee73fSGuenter Roeck 	[hwmon_fan_div] = "fan%d_div",
5948faee73fSGuenter Roeck 	[hwmon_fan_pulses] = "fan%d_pulses",
5958faee73fSGuenter Roeck 	[hwmon_fan_target] = "fan%d_target",
5968faee73fSGuenter Roeck 	[hwmon_fan_alarm] = "fan%d_alarm",
5978faee73fSGuenter Roeck 	[hwmon_fan_min_alarm] = "fan%d_min_alarm",
5988faee73fSGuenter Roeck 	[hwmon_fan_max_alarm] = "fan%d_max_alarm",
5998faee73fSGuenter Roeck 	[hwmon_fan_fault] = "fan%d_fault",
6008faee73fSGuenter Roeck };
6018faee73fSGuenter Roeck 
602f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = {
603f9f7bb3aSGuenter Roeck 	[hwmon_pwm_input] = "pwm%d",
604f9f7bb3aSGuenter Roeck 	[hwmon_pwm_enable] = "pwm%d_enable",
605f9f7bb3aSGuenter Roeck 	[hwmon_pwm_mode] = "pwm%d_mode",
606f9f7bb3aSGuenter Roeck 	[hwmon_pwm_freq] = "pwm%d_freq",
607f9f7bb3aSGuenter Roeck };
608f9f7bb3aSGuenter Roeck 
6094413405fSDr. David Alan Gilbert static const char * const hwmon_intrusion_attr_templates[] = {
6104413405fSDr. David Alan Gilbert 	[hwmon_intrusion_alarm] = "intrusion%d_alarm",
6114413405fSDr. David Alan Gilbert 	[hwmon_intrusion_beep]  = "intrusion%d_beep",
6124413405fSDr. David Alan Gilbert };
6134413405fSDr. David Alan Gilbert 
614d560168bSGuenter Roeck static const char * const *__templates[] = {
615f4d325d5SGuenter Roeck 	[hwmon_chip] = hwmon_chip_attrs,
616d560168bSGuenter Roeck 	[hwmon_temp] = hwmon_temp_attr_templates,
61700d616cfSGuenter Roeck 	[hwmon_in] = hwmon_in_attr_templates,
6189b26947cSGuenter Roeck 	[hwmon_curr] = hwmon_curr_attr_templates,
619b308f5c7SGuenter Roeck 	[hwmon_power] = hwmon_power_attr_templates,
6206bfcca44SGuenter Roeck 	[hwmon_energy] = hwmon_energy_attr_templates,
6216bfcca44SGuenter Roeck 	[hwmon_humidity] = hwmon_humidity_attr_templates,
6228faee73fSGuenter Roeck 	[hwmon_fan] = hwmon_fan_attr_templates,
623f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = hwmon_pwm_attr_templates,
6244413405fSDr. David Alan Gilbert 	[hwmon_intrusion] = hwmon_intrusion_attr_templates,
625d560168bSGuenter Roeck };
626d560168bSGuenter Roeck 
627d560168bSGuenter Roeck static const int __templates_size[] = {
628f4d325d5SGuenter Roeck 	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
629d560168bSGuenter Roeck 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
63000d616cfSGuenter Roeck 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
6319b26947cSGuenter Roeck 	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
632b308f5c7SGuenter Roeck 	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
6336bfcca44SGuenter Roeck 	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
6346bfcca44SGuenter Roeck 	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
6358faee73fSGuenter Roeck 	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
636f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
6374413405fSDr. David Alan Gilbert 	[hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
638d560168bSGuenter Roeck };
639d560168bSGuenter Roeck 
6401597b374SGuenter Roeck int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
6411597b374SGuenter Roeck 		       u32 attr, int channel)
6421597b374SGuenter Roeck {
6437f3cc8f8SGuenter Roeck 	char event[MAX_SYSFS_ATTR_NAME_LENGTH + 5];
6441597b374SGuenter Roeck 	char sattr[MAX_SYSFS_ATTR_NAME_LENGTH];
6457f3cc8f8SGuenter Roeck 	char *envp[] = { event, NULL };
6461597b374SGuenter Roeck 	const char * const *templates;
6471597b374SGuenter Roeck 	const char *template;
6481597b374SGuenter Roeck 	int base;
6491597b374SGuenter Roeck 
6501597b374SGuenter Roeck 	if (type >= ARRAY_SIZE(__templates))
6511597b374SGuenter Roeck 		return -EINVAL;
6521597b374SGuenter Roeck 	if (attr >= __templates_size[type])
6531597b374SGuenter Roeck 		return -EINVAL;
6541597b374SGuenter Roeck 
6551597b374SGuenter Roeck 	templates = __templates[type];
6561597b374SGuenter Roeck 	template = templates[attr];
6571597b374SGuenter Roeck 
6581597b374SGuenter Roeck 	base = hwmon_attr_base(type);
6591597b374SGuenter Roeck 
6601597b374SGuenter Roeck 	scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel);
6617f3cc8f8SGuenter Roeck 	scnprintf(event, sizeof(event), "NAME=%s", sattr);
6621597b374SGuenter Roeck 	sysfs_notify(&dev->kobj, NULL, sattr);
6637f3cc8f8SGuenter Roeck 	kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
6641597b374SGuenter Roeck 
6651597b374SGuenter Roeck 	if (type == hwmon_temp)
6661597b374SGuenter Roeck 		hwmon_thermal_notify(dev, channel);
6671597b374SGuenter Roeck 
6681597b374SGuenter Roeck 	return 0;
6691597b374SGuenter Roeck }
6701597b374SGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_notify_event);
6711597b374SGuenter Roeck 
672d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
673d560168bSGuenter Roeck {
674d560168bSGuenter Roeck 	int i, n;
675d560168bSGuenter Roeck 
676d560168bSGuenter Roeck 	for (i = n = 0; info->config[i]; i++)
677d560168bSGuenter Roeck 		n += hweight32(info->config[i]);
678d560168bSGuenter Roeck 
679d560168bSGuenter Roeck 	return n;
680d560168bSGuenter Roeck }
681d560168bSGuenter Roeck 
6823bf8bdcfSGuenter Roeck static int hwmon_genattrs(const void *drvdata,
683d560168bSGuenter Roeck 			  struct attribute **attrs,
684d560168bSGuenter Roeck 			  const struct hwmon_ops *ops,
685d560168bSGuenter Roeck 			  const struct hwmon_channel_info *info)
686d560168bSGuenter Roeck {
687d560168bSGuenter Roeck 	const char * const *templates;
688d560168bSGuenter Roeck 	int template_size;
689d560168bSGuenter Roeck 	int i, aindex = 0;
690d560168bSGuenter Roeck 
691d560168bSGuenter Roeck 	if (info->type >= ARRAY_SIZE(__templates))
692d560168bSGuenter Roeck 		return -EINVAL;
693d560168bSGuenter Roeck 
694d560168bSGuenter Roeck 	templates = __templates[info->type];
695d560168bSGuenter Roeck 	template_size = __templates_size[info->type];
696d560168bSGuenter Roeck 
697d560168bSGuenter Roeck 	for (i = 0; info->config[i]; i++) {
698d560168bSGuenter Roeck 		u32 attr_mask = info->config[i];
699d560168bSGuenter Roeck 		u32 attr;
700d560168bSGuenter Roeck 
701d560168bSGuenter Roeck 		while (attr_mask) {
702d560168bSGuenter Roeck 			struct attribute *a;
703d560168bSGuenter Roeck 
704d560168bSGuenter Roeck 			attr = __ffs(attr_mask);
705d560168bSGuenter Roeck 			attr_mask &= ~BIT(attr);
706d560168bSGuenter Roeck 			if (attr >= template_size)
707d560168bSGuenter Roeck 				return -EINVAL;
7083bf8bdcfSGuenter Roeck 			a = hwmon_genattr(drvdata, info->type, attr, i,
709d560168bSGuenter Roeck 					  templates[attr], ops);
710d560168bSGuenter Roeck 			if (IS_ERR(a)) {
711d560168bSGuenter Roeck 				if (PTR_ERR(a) != -ENOENT)
712d560168bSGuenter Roeck 					return PTR_ERR(a);
713d560168bSGuenter Roeck 				continue;
714d560168bSGuenter Roeck 			}
715d560168bSGuenter Roeck 			attrs[aindex++] = a;
716d560168bSGuenter Roeck 		}
717d560168bSGuenter Roeck 	}
718d560168bSGuenter Roeck 	return aindex;
719d560168bSGuenter Roeck }
720d560168bSGuenter Roeck 
721d560168bSGuenter Roeck static struct attribute **
7223bf8bdcfSGuenter Roeck __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
723d560168bSGuenter Roeck {
724d560168bSGuenter Roeck 	int ret, i, aindex = 0, nattrs = 0;
725d560168bSGuenter Roeck 	struct attribute **attrs;
726d560168bSGuenter Roeck 
727d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++)
728d560168bSGuenter Roeck 		nattrs += hwmon_num_channel_attrs(chip->info[i]);
729d560168bSGuenter Roeck 
730d560168bSGuenter Roeck 	if (nattrs == 0)
731d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
732d560168bSGuenter Roeck 
7333bf8bdcfSGuenter Roeck 	attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
734d560168bSGuenter Roeck 	if (!attrs)
735d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
736d560168bSGuenter Roeck 
737d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++) {
7383bf8bdcfSGuenter Roeck 		ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
739d560168bSGuenter Roeck 				     chip->info[i]);
7403bf8bdcfSGuenter Roeck 		if (ret < 0) {
7413bf8bdcfSGuenter Roeck 			hwmon_free_attrs(attrs);
742d560168bSGuenter Roeck 			return ERR_PTR(ret);
7433bf8bdcfSGuenter Roeck 		}
744d560168bSGuenter Roeck 		aindex += ret;
745d560168bSGuenter Roeck 	}
746d560168bSGuenter Roeck 
747d560168bSGuenter Roeck 	return attrs;
748d560168bSGuenter Roeck }
749d560168bSGuenter Roeck 
750d560168bSGuenter Roeck static struct device *
751d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
752d560168bSGuenter Roeck 			const struct hwmon_chip_info *chip,
753d560168bSGuenter Roeck 			const struct attribute_group **groups)
754d560168bSGuenter Roeck {
755d560168bSGuenter Roeck 	struct hwmon_device *hwdev;
756*e1c9d6d6SPaul Cercueil 	const char *label;
757d560168bSGuenter Roeck 	struct device *hdev;
75844e3ad88SAkinobu Mita 	int i, err, id;
759d560168bSGuenter Roeck 
76074d3b641SGuenter Roeck 	/* Complain about invalid characters in hwmon name attribute */
761d560168bSGuenter Roeck 	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
76274d3b641SGuenter Roeck 		dev_warn(dev,
76374d3b641SGuenter Roeck 			 "hwmon: '%s' is not a valid name attribute, please fix\n",
76474d3b641SGuenter Roeck 			 name);
765d560168bSGuenter Roeck 
766d560168bSGuenter Roeck 	id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
767d560168bSGuenter Roeck 	if (id < 0)
768d560168bSGuenter Roeck 		return ERR_PTR(id);
769d560168bSGuenter Roeck 
770d560168bSGuenter Roeck 	hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
771d560168bSGuenter Roeck 	if (hwdev == NULL) {
772d560168bSGuenter Roeck 		err = -ENOMEM;
773d560168bSGuenter Roeck 		goto ida_remove;
774d560168bSGuenter Roeck 	}
775d560168bSGuenter Roeck 
776d560168bSGuenter Roeck 	hdev = &hwdev->dev;
777d560168bSGuenter Roeck 
778239552f4SGuenter Roeck 	if (chip) {
779d560168bSGuenter Roeck 		struct attribute **attrs;
780b2a4cc3aSGuenter Roeck 		int ngroups = 2; /* terminating NULL plus &hwdev->groups */
781d560168bSGuenter Roeck 
782d560168bSGuenter Roeck 		if (groups)
783d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
784d560168bSGuenter Roeck 				ngroups++;
785d560168bSGuenter Roeck 
7863bf8bdcfSGuenter Roeck 		hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
78738d8ed65SColin Ian King 		if (!hwdev->groups) {
78838d8ed65SColin Ian King 			err = -ENOMEM;
78938d8ed65SColin Ian King 			goto free_hwmon;
79038d8ed65SColin Ian King 		}
791d560168bSGuenter Roeck 
7923bf8bdcfSGuenter Roeck 		attrs = __hwmon_create_attrs(drvdata, chip);
793d560168bSGuenter Roeck 		if (IS_ERR(attrs)) {
794d560168bSGuenter Roeck 			err = PTR_ERR(attrs);
795d560168bSGuenter Roeck 			goto free_hwmon;
796d560168bSGuenter Roeck 		}
797d560168bSGuenter Roeck 
798d560168bSGuenter Roeck 		hwdev->group.attrs = attrs;
799d560168bSGuenter Roeck 		ngroups = 0;
800d560168bSGuenter Roeck 		hwdev->groups[ngroups++] = &hwdev->group;
801d560168bSGuenter Roeck 
802d560168bSGuenter Roeck 		if (groups) {
803d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
804d560168bSGuenter Roeck 				hwdev->groups[ngroups++] = groups[i];
805d560168bSGuenter Roeck 		}
806d560168bSGuenter Roeck 
807d560168bSGuenter Roeck 		hdev->groups = hwdev->groups;
808d560168bSGuenter Roeck 	} else {
809d560168bSGuenter Roeck 		hdev->groups = groups;
810d560168bSGuenter Roeck 	}
811d560168bSGuenter Roeck 
812*e1c9d6d6SPaul Cercueil 	if (device_property_present(dev, "label")) {
813*e1c9d6d6SPaul Cercueil 		err = device_property_read_string(dev, "label", &label);
814*e1c9d6d6SPaul Cercueil 		if (err < 0)
815*e1c9d6d6SPaul Cercueil 			goto free_hwmon;
816*e1c9d6d6SPaul Cercueil 
817*e1c9d6d6SPaul Cercueil 		hwdev->label = kstrdup(label, GFP_KERNEL);
818*e1c9d6d6SPaul Cercueil 		if (hwdev->label == NULL) {
819*e1c9d6d6SPaul Cercueil 			err = -ENOMEM;
820*e1c9d6d6SPaul Cercueil 			goto free_hwmon;
821*e1c9d6d6SPaul Cercueil 		}
822*e1c9d6d6SPaul Cercueil 	}
823*e1c9d6d6SPaul Cercueil 
824d560168bSGuenter Roeck 	hwdev->name = name;
825d560168bSGuenter Roeck 	hdev->class = &hwmon_class;
826d560168bSGuenter Roeck 	hdev->parent = dev;
827d560168bSGuenter Roeck 	hdev->of_node = dev ? dev->of_node : NULL;
828d560168bSGuenter Roeck 	hwdev->chip = chip;
829d560168bSGuenter Roeck 	dev_set_drvdata(hdev, drvdata);
830d560168bSGuenter Roeck 	dev_set_name(hdev, HWMON_ID_FORMAT, id);
831d560168bSGuenter Roeck 	err = device_register(hdev);
832ada61aa0SYang Yingliang 	if (err) {
833ada61aa0SYang Yingliang 		put_device(hdev);
834ada61aa0SYang Yingliang 		goto ida_remove;
835ada61aa0SYang Yingliang 	}
836d560168bSGuenter Roeck 
8371597b374SGuenter Roeck 	INIT_LIST_HEAD(&hwdev->tzdata);
8381597b374SGuenter Roeck 
839c41dd48eSEduardo Valentin 	if (dev && dev->of_node && chip && chip->ops->read &&
840d560168bSGuenter Roeck 	    chip->info[0]->type == hwmon_chip &&
841d560168bSGuenter Roeck 	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
84244e3ad88SAkinobu Mita 		err = hwmon_thermal_register_sensors(hdev);
84374e35127SDmitry Osipenko 		if (err) {
84474e35127SDmitry Osipenko 			device_unregister(hdev);
845792eac18SGuenter Roeck 			/*
84644e3ad88SAkinobu Mita 			 * Don't worry about hwdev; hwmon_dev_release(), called
84744e3ad88SAkinobu Mita 			 * from device_unregister(), will free it.
848792eac18SGuenter Roeck 			 */
84974e35127SDmitry Osipenko 			goto ida_remove;
85074e35127SDmitry Osipenko 		}
85147c332deSLinus Walleij 	}
852d560168bSGuenter Roeck 
853d560168bSGuenter Roeck 	return hdev;
854d560168bSGuenter Roeck 
855d560168bSGuenter Roeck free_hwmon:
8563bf8bdcfSGuenter Roeck 	hwmon_dev_release(hdev);
857d560168bSGuenter Roeck ida_remove:
858d560168bSGuenter Roeck 	ida_simple_remove(&hwmon_ida, id);
859d560168bSGuenter Roeck 	return ERR_PTR(err);
860d560168bSGuenter Roeck }
861d560168bSGuenter Roeck 
8621236441fSMark M. Hoffman /**
863bab2243cSGuenter Roeck  * hwmon_device_register_with_groups - register w/ hwmon
864bab2243cSGuenter Roeck  * @dev: the parent device
865bab2243cSGuenter Roeck  * @name: hwmon name attribute
866bab2243cSGuenter Roeck  * @drvdata: driver data to attach to created device
867bab2243cSGuenter Roeck  * @groups: List of attribute groups to create
868bab2243cSGuenter Roeck  *
869bab2243cSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
870bab2243cSGuenter Roeck  * longer needed.
871bab2243cSGuenter Roeck  *
872bab2243cSGuenter Roeck  * Returns the pointer to the new device.
873bab2243cSGuenter Roeck  */
874bab2243cSGuenter Roeck struct device *
875bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name,
876bab2243cSGuenter Roeck 				  void *drvdata,
877bab2243cSGuenter Roeck 				  const struct attribute_group **groups)
878bab2243cSGuenter Roeck {
8798353863aSGuenter Roeck 	if (!name)
8808353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
8818353863aSGuenter Roeck 
882d560168bSGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
883bab2243cSGuenter Roeck }
884bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
885bab2243cSGuenter Roeck 
886bab2243cSGuenter Roeck /**
887d560168bSGuenter Roeck  * hwmon_device_register_with_info - register w/ hwmon
888d560168bSGuenter Roeck  * @dev: the parent device
889d560168bSGuenter Roeck  * @name: hwmon name attribute
890d560168bSGuenter Roeck  * @drvdata: driver data to attach to created device
8913870945aSGuenter Roeck  * @chip: pointer to hwmon chip information
892848ba0a2SGuenter Roeck  * @extra_groups: pointer to list of additional non-standard attribute groups
893d560168bSGuenter Roeck  *
894d560168bSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
895d560168bSGuenter Roeck  * longer needed.
896d560168bSGuenter Roeck  *
897d560168bSGuenter Roeck  * Returns the pointer to the new device.
898d560168bSGuenter Roeck  */
899d560168bSGuenter Roeck struct device *
900d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name,
901d560168bSGuenter Roeck 				void *drvdata,
902d560168bSGuenter Roeck 				const struct hwmon_chip_info *chip,
903848ba0a2SGuenter Roeck 				const struct attribute_group **extra_groups)
904d560168bSGuenter Roeck {
9058353863aSGuenter Roeck 	if (!name)
9068353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
9078353863aSGuenter Roeck 
908239552f4SGuenter Roeck 	if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
909d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
910d560168bSGuenter Roeck 
91159df4f4eSLucas Magasweran 	if (chip && !dev)
91259df4f4eSLucas Magasweran 		return ERR_PTR(-EINVAL);
91359df4f4eSLucas Magasweran 
914848ba0a2SGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
915d560168bSGuenter Roeck }
916d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
917d560168bSGuenter Roeck 
918d560168bSGuenter Roeck /**
9191beeffe4STony Jones  * hwmon_device_register - register w/ hwmon
9201236441fSMark M. Hoffman  * @dev: the device to register
9211236441fSMark M. Hoffman  *
9221beeffe4STony Jones  * hwmon_device_unregister() must be called when the device is no
9231236441fSMark M. Hoffman  * longer needed.
9241236441fSMark M. Hoffman  *
9251beeffe4STony Jones  * Returns the pointer to the new device.
9261236441fSMark M. Hoffman  */
9271beeffe4STony Jones struct device *hwmon_device_register(struct device *dev)
9281236441fSMark M. Hoffman {
929af1bd36cSGuenter Roeck 	dev_warn(dev,
930af1bd36cSGuenter Roeck 		 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
931af1bd36cSGuenter Roeck 
9328353863aSGuenter Roeck 	return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
9331236441fSMark M. Hoffman }
934839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register);
9351236441fSMark M. Hoffman 
9361236441fSMark M. Hoffman /**
9371236441fSMark M. Hoffman  * hwmon_device_unregister - removes the previously registered class device
9381236441fSMark M. Hoffman  *
9391beeffe4STony Jones  * @dev: the class device to destroy
9401236441fSMark M. Hoffman  */
9411beeffe4STony Jones void hwmon_device_unregister(struct device *dev)
9421236441fSMark M. Hoffman {
9431236441fSMark M. Hoffman 	int id;
9441236441fSMark M. Hoffman 
945739cf3a2SKay Sievers 	if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
9461beeffe4STony Jones 		device_unregister(dev);
9474ca5f468SJonathan Cameron 		ida_simple_remove(&hwmon_ida, id);
9481236441fSMark M. Hoffman 	} else
9491beeffe4STony Jones 		dev_dbg(dev->parent,
9501236441fSMark M. Hoffman 			"hwmon_device_unregister() failed: bad class ID!\n");
9511236441fSMark M. Hoffman }
952839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister);
9531236441fSMark M. Hoffman 
95474188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res)
95574188cbaSGuenter Roeck {
95674188cbaSGuenter Roeck 	struct device *hwdev = *(struct device **)res;
95774188cbaSGuenter Roeck 
95874188cbaSGuenter Roeck 	hwmon_device_unregister(hwdev);
95974188cbaSGuenter Roeck }
96074188cbaSGuenter Roeck 
96174188cbaSGuenter Roeck /**
96274188cbaSGuenter Roeck  * devm_hwmon_device_register_with_groups - register w/ hwmon
96374188cbaSGuenter Roeck  * @dev: the parent device
96474188cbaSGuenter Roeck  * @name: hwmon name attribute
96574188cbaSGuenter Roeck  * @drvdata: driver data to attach to created device
96674188cbaSGuenter Roeck  * @groups: List of attribute groups to create
96774188cbaSGuenter Roeck  *
96874188cbaSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
96974188cbaSGuenter Roeck  * unregistered with the parent device.
97074188cbaSGuenter Roeck  */
97174188cbaSGuenter Roeck struct device *
97274188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
97374188cbaSGuenter Roeck 				       void *drvdata,
97474188cbaSGuenter Roeck 				       const struct attribute_group **groups)
97574188cbaSGuenter Roeck {
97674188cbaSGuenter Roeck 	struct device **ptr, *hwdev;
97774188cbaSGuenter Roeck 
97874188cbaSGuenter Roeck 	if (!dev)
97974188cbaSGuenter Roeck 		return ERR_PTR(-EINVAL);
98074188cbaSGuenter Roeck 
98174188cbaSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
98274188cbaSGuenter Roeck 	if (!ptr)
98374188cbaSGuenter Roeck 		return ERR_PTR(-ENOMEM);
98474188cbaSGuenter Roeck 
98574188cbaSGuenter Roeck 	hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
98674188cbaSGuenter Roeck 	if (IS_ERR(hwdev))
98774188cbaSGuenter Roeck 		goto error;
98874188cbaSGuenter Roeck 
98974188cbaSGuenter Roeck 	*ptr = hwdev;
99074188cbaSGuenter Roeck 	devres_add(dev, ptr);
99174188cbaSGuenter Roeck 	return hwdev;
99274188cbaSGuenter Roeck 
99374188cbaSGuenter Roeck error:
99474188cbaSGuenter Roeck 	devres_free(ptr);
99574188cbaSGuenter Roeck 	return hwdev;
99674188cbaSGuenter Roeck }
99774188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
99874188cbaSGuenter Roeck 
999d560168bSGuenter Roeck /**
1000d560168bSGuenter Roeck  * devm_hwmon_device_register_with_info - register w/ hwmon
1001d560168bSGuenter Roeck  * @dev:	the parent device
1002d560168bSGuenter Roeck  * @name:	hwmon name attribute
1003d560168bSGuenter Roeck  * @drvdata:	driver data to attach to created device
10043870945aSGuenter Roeck  * @chip:	pointer to hwmon chip information
10053870945aSGuenter Roeck  * @groups:	pointer to list of driver specific attribute groups
1006d560168bSGuenter Roeck  *
1007d560168bSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
1008d560168bSGuenter Roeck  * unregistered with the parent device.
1009d560168bSGuenter Roeck  */
1010d560168bSGuenter Roeck struct device *
1011d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name,
1012d560168bSGuenter Roeck 				     void *drvdata,
1013d560168bSGuenter Roeck 				     const struct hwmon_chip_info *chip,
1014d560168bSGuenter Roeck 				     const struct attribute_group **groups)
1015d560168bSGuenter Roeck {
1016d560168bSGuenter Roeck 	struct device **ptr, *hwdev;
1017d560168bSGuenter Roeck 
1018d560168bSGuenter Roeck 	if (!dev)
1019d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
1020d560168bSGuenter Roeck 
1021d560168bSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
1022d560168bSGuenter Roeck 	if (!ptr)
1023d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
1024d560168bSGuenter Roeck 
1025d560168bSGuenter Roeck 	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
1026d560168bSGuenter Roeck 						groups);
1027d560168bSGuenter Roeck 	if (IS_ERR(hwdev))
1028d560168bSGuenter Roeck 		goto error;
1029d560168bSGuenter Roeck 
1030d560168bSGuenter Roeck 	*ptr = hwdev;
1031d560168bSGuenter Roeck 	devres_add(dev, ptr);
1032d560168bSGuenter Roeck 
1033d560168bSGuenter Roeck 	return hwdev;
1034d560168bSGuenter Roeck 
1035d560168bSGuenter Roeck error:
1036d560168bSGuenter Roeck 	devres_free(ptr);
1037d560168bSGuenter Roeck 	return hwdev;
1038d560168bSGuenter Roeck }
1039d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
1040d560168bSGuenter Roeck 
104174188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data)
104274188cbaSGuenter Roeck {
104374188cbaSGuenter Roeck 	struct device **hwdev = res;
104474188cbaSGuenter Roeck 
104574188cbaSGuenter Roeck 	return *hwdev == data;
104674188cbaSGuenter Roeck }
104774188cbaSGuenter Roeck 
104874188cbaSGuenter Roeck /**
104974188cbaSGuenter Roeck  * devm_hwmon_device_unregister - removes a previously registered hwmon device
105074188cbaSGuenter Roeck  *
105174188cbaSGuenter Roeck  * @dev: the parent device of the device to unregister
105274188cbaSGuenter Roeck  */
105374188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev)
105474188cbaSGuenter Roeck {
105574188cbaSGuenter Roeck 	WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
105674188cbaSGuenter Roeck }
105774188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
105874188cbaSGuenter Roeck 
10592958b1ecSJean Delvare static void __init hwmon_pci_quirks(void)
10602958b1ecSJean Delvare {
10612958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI
10622958b1ecSJean Delvare 	struct pci_dev *sb;
10632958b1ecSJean Delvare 	u16 base;
10642958b1ecSJean Delvare 	u8 enable;
10652958b1ecSJean Delvare 
10662958b1ecSJean Delvare 	/* Open access to 0x295-0x296 on MSI MS-7031 */
10672958b1ecSJean Delvare 	sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
1068d6dab7ddSJean Delvare 	if (sb) {
1069d6dab7ddSJean Delvare 		if (sb->subsystem_vendor == 0x1462 &&	/* MSI */
1070d6dab7ddSJean Delvare 		    sb->subsystem_device == 0x0031) {	/* MS-7031 */
10712958b1ecSJean Delvare 			pci_read_config_byte(sb, 0x48, &enable);
10722958b1ecSJean Delvare 			pci_read_config_word(sb, 0x64, &base);
10732958b1ecSJean Delvare 
10742958b1ecSJean Delvare 			if (base == 0 && !(enable & BIT(2))) {
10752958b1ecSJean Delvare 				dev_info(&sb->dev,
10762958b1ecSJean Delvare 					 "Opening wide generic port at 0x295\n");
10772958b1ecSJean Delvare 				pci_write_config_word(sb, 0x64, 0x295);
1078d6dab7ddSJean Delvare 				pci_write_config_byte(sb, 0x48,
1079d6dab7ddSJean Delvare 						      enable | BIT(2));
10802958b1ecSJean Delvare 			}
10812958b1ecSJean Delvare 		}
1082d6dab7ddSJean Delvare 		pci_dev_put(sb);
1083d6dab7ddSJean Delvare 	}
10842958b1ecSJean Delvare #endif
10852958b1ecSJean Delvare }
10862958b1ecSJean Delvare 
10871236441fSMark M. Hoffman static int __init hwmon_init(void)
10881236441fSMark M. Hoffman {
1089bab2243cSGuenter Roeck 	int err;
1090bab2243cSGuenter Roeck 
10912958b1ecSJean Delvare 	hwmon_pci_quirks();
10922958b1ecSJean Delvare 
1093bab2243cSGuenter Roeck 	err = class_register(&hwmon_class);
1094bab2243cSGuenter Roeck 	if (err) {
1095bab2243cSGuenter Roeck 		pr_err("couldn't register hwmon sysfs class\n");
1096bab2243cSGuenter Roeck 		return err;
10971236441fSMark M. Hoffman 	}
10981236441fSMark M. Hoffman 	return 0;
10991236441fSMark M. Hoffman }
11001236441fSMark M. Hoffman 
11011236441fSMark M. Hoffman static void __exit hwmon_exit(void)
11021236441fSMark M. Hoffman {
1103bab2243cSGuenter Roeck 	class_unregister(&hwmon_class);
11041236441fSMark M. Hoffman }
11051236441fSMark M. Hoffman 
110637f54ee5SDavid Brownell subsys_initcall(hwmon_init);
11071236441fSMark M. Hoffman module_exit(hwmon_exit);
11081236441fSMark M. Hoffman 
11091236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
11101236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
11111236441fSMark M. Hoffman MODULE_LICENSE("GPL");
11121236441fSMark M. Hoffman 
1113