xref: /linux/drivers/hwmon/hwmon.c (revision 7f3cc8f897634b7e2d79bc2b7105e9ae6eaf4ac2)
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>
21c9ebbe6fSGuenter Roeck #include <linux/slab.h>
22648cd48cSGuenter Roeck #include <linux/string.h>
23d560168bSGuenter Roeck #include <linux/thermal.h>
241236441fSMark M. Hoffman 
2561b8ab2cSNicolin Chen #define CREATE_TRACE_POINTS
2661b8ab2cSNicolin Chen #include <trace/events/hwmon.h>
2761b8ab2cSNicolin Chen 
281236441fSMark M. Hoffman #define HWMON_ID_PREFIX "hwmon"
291236441fSMark M. Hoffman #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
301236441fSMark M. Hoffman 
31bab2243cSGuenter Roeck struct hwmon_device {
32bab2243cSGuenter Roeck 	const char *name;
33bab2243cSGuenter Roeck 	struct device dev;
34d560168bSGuenter Roeck 	const struct hwmon_chip_info *chip;
351597b374SGuenter Roeck 	struct list_head tzdata;
36d560168bSGuenter Roeck 	struct attribute_group group;
37d560168bSGuenter Roeck 	const struct attribute_group **groups;
38bab2243cSGuenter Roeck };
39d560168bSGuenter Roeck 
40bab2243cSGuenter Roeck #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
41bab2243cSGuenter Roeck 
423a412d5eSGuenter Roeck #define MAX_SYSFS_ATTR_NAME_LENGTH	32
433a412d5eSGuenter Roeck 
44d560168bSGuenter Roeck struct hwmon_device_attribute {
45d560168bSGuenter Roeck 	struct device_attribute dev_attr;
46d560168bSGuenter Roeck 	const struct hwmon_ops *ops;
47d560168bSGuenter Roeck 	enum hwmon_sensor_types type;
48d560168bSGuenter Roeck 	u32 attr;
49d560168bSGuenter Roeck 	int index;
503a412d5eSGuenter Roeck 	char name[MAX_SYSFS_ATTR_NAME_LENGTH];
51d560168bSGuenter Roeck };
52d560168bSGuenter Roeck 
53d560168bSGuenter Roeck #define to_hwmon_attr(d) \
54d560168bSGuenter Roeck 	container_of(d, struct hwmon_device_attribute, dev_attr)
553bf8bdcfSGuenter Roeck #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
56d560168bSGuenter Roeck 
57d560168bSGuenter Roeck /*
58d560168bSGuenter Roeck  * Thermal zone information
59d560168bSGuenter Roeck  */
60d560168bSGuenter Roeck struct hwmon_thermal_data {
611597b374SGuenter Roeck 	struct list_head node;		/* hwmon tzdata list entry */
623bf8bdcfSGuenter Roeck 	struct device *dev;		/* Reference to hwmon device */
63d560168bSGuenter Roeck 	int index;			/* sensor index */
641597b374SGuenter Roeck 	struct thermal_zone_device *tzd;/* thermal zone device */
65d560168bSGuenter Roeck };
66d560168bSGuenter Roeck 
67bab2243cSGuenter Roeck static ssize_t
682ab0c6c5SJulia Lawall name_show(struct device *dev, struct device_attribute *attr, char *buf)
69bab2243cSGuenter Roeck {
70bab2243cSGuenter Roeck 	return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
71bab2243cSGuenter Roeck }
722ab0c6c5SJulia Lawall static DEVICE_ATTR_RO(name);
73bab2243cSGuenter Roeck 
74bab2243cSGuenter Roeck static struct attribute *hwmon_dev_attrs[] = {
75bab2243cSGuenter Roeck 	&dev_attr_name.attr,
76bab2243cSGuenter Roeck 	NULL
77bab2243cSGuenter Roeck };
78bab2243cSGuenter Roeck 
79bab2243cSGuenter Roeck static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
80bab2243cSGuenter Roeck 					 struct attribute *attr, int n)
81bab2243cSGuenter Roeck {
8277d76768SYang Li 	struct device *dev = kobj_to_dev(kobj);
83bab2243cSGuenter Roeck 
84bab2243cSGuenter Roeck 	if (to_hwmon_device(dev)->name == NULL)
85bab2243cSGuenter Roeck 		return 0;
86bab2243cSGuenter Roeck 
87bab2243cSGuenter Roeck 	return attr->mode;
88bab2243cSGuenter Roeck }
89bab2243cSGuenter Roeck 
90524703acSArvind Yadav static const struct attribute_group hwmon_dev_attr_group = {
91bab2243cSGuenter Roeck 	.attrs		= hwmon_dev_attrs,
92bab2243cSGuenter Roeck 	.is_visible	= hwmon_dev_name_is_visible,
93bab2243cSGuenter Roeck };
94bab2243cSGuenter Roeck 
95bab2243cSGuenter Roeck static const struct attribute_group *hwmon_dev_attr_groups[] = {
96bab2243cSGuenter Roeck 	&hwmon_dev_attr_group,
97bab2243cSGuenter Roeck 	NULL
98bab2243cSGuenter Roeck };
99bab2243cSGuenter Roeck 
1003bf8bdcfSGuenter Roeck static void hwmon_free_attrs(struct attribute **attrs)
1013bf8bdcfSGuenter Roeck {
1023bf8bdcfSGuenter Roeck 	int i;
1033bf8bdcfSGuenter Roeck 
1043bf8bdcfSGuenter Roeck 	for (i = 0; attrs[i]; i++) {
1053bf8bdcfSGuenter Roeck 		struct device_attribute *dattr = to_dev_attr(attrs[i]);
1063bf8bdcfSGuenter Roeck 		struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr);
1073bf8bdcfSGuenter Roeck 
1083bf8bdcfSGuenter Roeck 		kfree(hattr);
1093bf8bdcfSGuenter Roeck 	}
1103bf8bdcfSGuenter Roeck 	kfree(attrs);
1113bf8bdcfSGuenter Roeck }
1123bf8bdcfSGuenter Roeck 
113bab2243cSGuenter Roeck static void hwmon_dev_release(struct device *dev)
114bab2243cSGuenter Roeck {
1153bf8bdcfSGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(dev);
1163bf8bdcfSGuenter Roeck 
1173bf8bdcfSGuenter Roeck 	if (hwdev->group.attrs)
1183bf8bdcfSGuenter Roeck 		hwmon_free_attrs(hwdev->group.attrs);
1193bf8bdcfSGuenter Roeck 	kfree(hwdev->groups);
1203bf8bdcfSGuenter Roeck 	kfree(hwdev);
121bab2243cSGuenter Roeck }
122bab2243cSGuenter Roeck 
123bab2243cSGuenter Roeck static struct class hwmon_class = {
124bab2243cSGuenter Roeck 	.name = "hwmon",
125bab2243cSGuenter Roeck 	.owner = THIS_MODULE,
126bab2243cSGuenter Roeck 	.dev_groups = hwmon_dev_attr_groups,
127bab2243cSGuenter Roeck 	.dev_release = hwmon_dev_release,
128bab2243cSGuenter Roeck };
1291236441fSMark M. Hoffman 
1304ca5f468SJonathan Cameron static DEFINE_IDA(hwmon_ida);
1311236441fSMark M. Hoffman 
132d560168bSGuenter Roeck /* Thermal zone handling */
133d560168bSGuenter Roeck 
13486430c1aSGuenter Roeck /*
13586430c1aSGuenter Roeck  * The complex conditional is necessary to avoid a cyclic dependency
13686430c1aSGuenter Roeck  * between hwmon and thermal_sys modules.
13786430c1aSGuenter Roeck  */
138f3735332SDaniel Lezcano #ifdef CONFIG_THERMAL_OF
139d560168bSGuenter Roeck static int hwmon_thermal_get_temp(void *data, int *temp)
140d560168bSGuenter Roeck {
141d560168bSGuenter Roeck 	struct hwmon_thermal_data *tdata = data;
1423bf8bdcfSGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
143d560168bSGuenter Roeck 	int ret;
144d560168bSGuenter Roeck 	long t;
145d560168bSGuenter Roeck 
1463bf8bdcfSGuenter Roeck 	ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
147d560168bSGuenter Roeck 				     tdata->index, &t);
148d560168bSGuenter Roeck 	if (ret < 0)
149d560168bSGuenter Roeck 		return ret;
150d560168bSGuenter Roeck 
151d560168bSGuenter Roeck 	*temp = t;
152d560168bSGuenter Roeck 
153d560168bSGuenter Roeck 	return 0;
154d560168bSGuenter Roeck }
155d560168bSGuenter Roeck 
156a5f6c0f8SDmitry Osipenko static int hwmon_thermal_set_trips(void *data, int low, int high)
157a5f6c0f8SDmitry Osipenko {
158a5f6c0f8SDmitry Osipenko 	struct hwmon_thermal_data *tdata = data;
159a5f6c0f8SDmitry Osipenko 	struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
160a5f6c0f8SDmitry Osipenko 	const struct hwmon_chip_info *chip = hwdev->chip;
161a5f6c0f8SDmitry Osipenko 	const struct hwmon_channel_info **info = chip->info;
162a5f6c0f8SDmitry Osipenko 	unsigned int i;
163a5f6c0f8SDmitry Osipenko 	int err;
164a5f6c0f8SDmitry Osipenko 
165a5f6c0f8SDmitry Osipenko 	if (!chip->ops->write)
166a5f6c0f8SDmitry Osipenko 		return 0;
167a5f6c0f8SDmitry Osipenko 
168a5f6c0f8SDmitry Osipenko 	for (i = 0; info[i] && info[i]->type != hwmon_temp; i++)
169a5f6c0f8SDmitry Osipenko 		continue;
170a5f6c0f8SDmitry Osipenko 
171a5f6c0f8SDmitry Osipenko 	if (!info[i])
172a5f6c0f8SDmitry Osipenko 		return 0;
173a5f6c0f8SDmitry Osipenko 
174a5f6c0f8SDmitry Osipenko 	if (info[i]->config[tdata->index] & HWMON_T_MIN) {
175a5f6c0f8SDmitry Osipenko 		err = chip->ops->write(tdata->dev, hwmon_temp,
176a5f6c0f8SDmitry Osipenko 				       hwmon_temp_min, tdata->index, low);
177a5f6c0f8SDmitry Osipenko 		if (err && err != -EOPNOTSUPP)
178a5f6c0f8SDmitry Osipenko 			return err;
179a5f6c0f8SDmitry Osipenko 	}
180a5f6c0f8SDmitry Osipenko 
181a5f6c0f8SDmitry Osipenko 	if (info[i]->config[tdata->index] & HWMON_T_MAX) {
182a5f6c0f8SDmitry Osipenko 		err = chip->ops->write(tdata->dev, hwmon_temp,
183a5f6c0f8SDmitry Osipenko 				       hwmon_temp_max, tdata->index, high);
184a5f6c0f8SDmitry Osipenko 		if (err && err != -EOPNOTSUPP)
185a5f6c0f8SDmitry Osipenko 			return err;
186a5f6c0f8SDmitry Osipenko 	}
187a5f6c0f8SDmitry Osipenko 
188a5f6c0f8SDmitry Osipenko 	return 0;
189a5f6c0f8SDmitry Osipenko }
190a5f6c0f8SDmitry Osipenko 
191c9920650SJulia Lawall static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
192d560168bSGuenter Roeck 	.get_temp = hwmon_thermal_get_temp,
193a5f6c0f8SDmitry Osipenko 	.set_trips = hwmon_thermal_set_trips,
194d560168bSGuenter Roeck };
195d560168bSGuenter Roeck 
1961597b374SGuenter Roeck static void hwmon_thermal_remove_sensor(void *data)
1971597b374SGuenter Roeck {
1981597b374SGuenter Roeck 	list_del(data);
1991597b374SGuenter Roeck }
2001597b374SGuenter Roeck 
2013bf8bdcfSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, int index)
202d560168bSGuenter Roeck {
2031597b374SGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(dev);
204d560168bSGuenter Roeck 	struct hwmon_thermal_data *tdata;
20547c332deSLinus Walleij 	struct thermal_zone_device *tzd;
2061597b374SGuenter Roeck 	int err;
207d560168bSGuenter Roeck 
208d560168bSGuenter Roeck 	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
209d560168bSGuenter Roeck 	if (!tdata)
210d560168bSGuenter Roeck 		return -ENOMEM;
211d560168bSGuenter Roeck 
2123bf8bdcfSGuenter Roeck 	tdata->dev = dev;
213d560168bSGuenter Roeck 	tdata->index = index;
214d560168bSGuenter Roeck 
2153bf8bdcfSGuenter Roeck 	tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata,
216d560168bSGuenter Roeck 						   &hwmon_thermal_ops);
2171b5f517cSGuenter Roeck 	if (IS_ERR(tzd)) {
2181b5f517cSGuenter Roeck 		if (PTR_ERR(tzd) != -ENODEV)
21947c332deSLinus Walleij 			return PTR_ERR(tzd);
2201b5f517cSGuenter Roeck 		dev_info(dev, "temp%d_input not attached to any thermal zone\n",
2211b5f517cSGuenter Roeck 			 index + 1);
2221b5f517cSGuenter Roeck 		devm_kfree(dev, tdata);
2231b5f517cSGuenter Roeck 		return 0;
2241b5f517cSGuenter Roeck 	}
225d560168bSGuenter Roeck 
2261597b374SGuenter Roeck 	err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node);
2271597b374SGuenter Roeck 	if (err)
2281597b374SGuenter Roeck 		return err;
2291597b374SGuenter Roeck 
2301597b374SGuenter Roeck 	tdata->tzd = tzd;
2311597b374SGuenter Roeck 	list_add(&tdata->node, &hwdev->tzdata);
2321597b374SGuenter Roeck 
233d560168bSGuenter Roeck 	return 0;
234d560168bSGuenter Roeck }
23544e3ad88SAkinobu Mita 
23644e3ad88SAkinobu Mita static int hwmon_thermal_register_sensors(struct device *dev)
23744e3ad88SAkinobu Mita {
23844e3ad88SAkinobu Mita 	struct hwmon_device *hwdev = to_hwmon_device(dev);
23944e3ad88SAkinobu Mita 	const struct hwmon_chip_info *chip = hwdev->chip;
24044e3ad88SAkinobu Mita 	const struct hwmon_channel_info **info = chip->info;
24144e3ad88SAkinobu Mita 	void *drvdata = dev_get_drvdata(dev);
24244e3ad88SAkinobu Mita 	int i;
24344e3ad88SAkinobu Mita 
24444e3ad88SAkinobu Mita 	for (i = 1; info[i]; i++) {
24544e3ad88SAkinobu Mita 		int j;
24644e3ad88SAkinobu Mita 
24744e3ad88SAkinobu Mita 		if (info[i]->type != hwmon_temp)
24844e3ad88SAkinobu Mita 			continue;
24944e3ad88SAkinobu Mita 
25044e3ad88SAkinobu Mita 		for (j = 0; info[i]->config[j]; j++) {
25144e3ad88SAkinobu Mita 			int err;
25244e3ad88SAkinobu Mita 
25344e3ad88SAkinobu Mita 			if (!(info[i]->config[j] & HWMON_T_INPUT) ||
25444e3ad88SAkinobu Mita 			    !chip->ops->is_visible(drvdata, hwmon_temp,
25544e3ad88SAkinobu Mita 						   hwmon_temp_input, j))
25644e3ad88SAkinobu Mita 				continue;
25744e3ad88SAkinobu Mita 
25844e3ad88SAkinobu Mita 			err = hwmon_thermal_add_sensor(dev, j);
25944e3ad88SAkinobu Mita 			if (err)
26044e3ad88SAkinobu Mita 				return err;
26144e3ad88SAkinobu Mita 		}
26244e3ad88SAkinobu Mita 	}
26344e3ad88SAkinobu Mita 
26444e3ad88SAkinobu Mita 	return 0;
26544e3ad88SAkinobu Mita }
26644e3ad88SAkinobu Mita 
2671597b374SGuenter Roeck static void hwmon_thermal_notify(struct device *dev, int index)
2681597b374SGuenter Roeck {
2691597b374SGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(dev);
2701597b374SGuenter Roeck 	struct hwmon_thermal_data *tzdata;
2711597b374SGuenter Roeck 
2721597b374SGuenter Roeck 	list_for_each_entry(tzdata, &hwdev->tzdata, node) {
2731597b374SGuenter Roeck 		if (tzdata->index == index) {
2741597b374SGuenter Roeck 			thermal_zone_device_update(tzdata->tzd,
2751597b374SGuenter Roeck 						   THERMAL_EVENT_UNSPECIFIED);
2761597b374SGuenter Roeck 		}
2771597b374SGuenter Roeck 	}
2781597b374SGuenter Roeck }
2791597b374SGuenter Roeck 
280d560168bSGuenter Roeck #else
28144e3ad88SAkinobu Mita static int hwmon_thermal_register_sensors(struct device *dev)
282d560168bSGuenter Roeck {
283d560168bSGuenter Roeck 	return 0;
284d560168bSGuenter Roeck }
2851597b374SGuenter Roeck 
2861597b374SGuenter Roeck static void hwmon_thermal_notify(struct device *dev, int index) { }
2871597b374SGuenter Roeck 
28886430c1aSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
289d560168bSGuenter Roeck 
29061b8ab2cSNicolin Chen static int hwmon_attr_base(enum hwmon_sensor_types type)
29161b8ab2cSNicolin Chen {
2924413405fSDr. David Alan Gilbert 	if (type == hwmon_in || type == hwmon_intrusion)
29361b8ab2cSNicolin Chen 		return 0;
29461b8ab2cSNicolin Chen 	return 1;
29561b8ab2cSNicolin Chen }
29661b8ab2cSNicolin Chen 
297d560168bSGuenter Roeck /* sysfs attribute management */
298d560168bSGuenter Roeck 
299d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev,
300d560168bSGuenter Roeck 			       struct device_attribute *devattr, char *buf)
301d560168bSGuenter Roeck {
302d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
303d560168bSGuenter Roeck 	long val;
304d560168bSGuenter Roeck 	int ret;
305d560168bSGuenter Roeck 
306d560168bSGuenter Roeck 	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
307d560168bSGuenter Roeck 			       &val);
308d560168bSGuenter Roeck 	if (ret < 0)
309d560168bSGuenter Roeck 		return ret;
310d560168bSGuenter Roeck 
31161b8ab2cSNicolin Chen 	trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
31261b8ab2cSNicolin Chen 			      hattr->name, val);
31361b8ab2cSNicolin Chen 
314d560168bSGuenter Roeck 	return sprintf(buf, "%ld\n", val);
315d560168bSGuenter Roeck }
316d560168bSGuenter Roeck 
317e159ab5cSGuenter Roeck static ssize_t hwmon_attr_show_string(struct device *dev,
318e159ab5cSGuenter Roeck 				      struct device_attribute *devattr,
319e159ab5cSGuenter Roeck 				      char *buf)
320e159ab5cSGuenter Roeck {
321e159ab5cSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
32261b8ab2cSNicolin Chen 	enum hwmon_sensor_types type = hattr->type;
3235ba6bcbcSJean Delvare 	const char *s;
324e159ab5cSGuenter Roeck 	int ret;
325e159ab5cSGuenter Roeck 
326e159ab5cSGuenter Roeck 	ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
327e159ab5cSGuenter Roeck 				      hattr->index, &s);
328e159ab5cSGuenter Roeck 	if (ret < 0)
329e159ab5cSGuenter Roeck 		return ret;
330e159ab5cSGuenter Roeck 
33161b8ab2cSNicolin Chen 	trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
33261b8ab2cSNicolin Chen 				     hattr->name, s);
33361b8ab2cSNicolin Chen 
334e159ab5cSGuenter Roeck 	return sprintf(buf, "%s\n", s);
335e159ab5cSGuenter Roeck }
336e159ab5cSGuenter Roeck 
337d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev,
338d560168bSGuenter Roeck 				struct device_attribute *devattr,
339d560168bSGuenter Roeck 				const char *buf, size_t count)
340d560168bSGuenter Roeck {
341d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
342d560168bSGuenter Roeck 	long val;
343d560168bSGuenter Roeck 	int ret;
344d560168bSGuenter Roeck 
345d560168bSGuenter Roeck 	ret = kstrtol(buf, 10, &val);
346d560168bSGuenter Roeck 	if (ret < 0)
347d560168bSGuenter Roeck 		return ret;
348d560168bSGuenter Roeck 
349d560168bSGuenter Roeck 	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
350d560168bSGuenter Roeck 				val);
351d560168bSGuenter Roeck 	if (ret < 0)
352d560168bSGuenter Roeck 		return ret;
353d560168bSGuenter Roeck 
35461b8ab2cSNicolin Chen 	trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
35561b8ab2cSNicolin Chen 			       hattr->name, val);
356d560168bSGuenter Roeck 
35761b8ab2cSNicolin Chen 	return count;
358d560168bSGuenter Roeck }
359d560168bSGuenter Roeck 
360e159ab5cSGuenter Roeck static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
361e159ab5cSGuenter Roeck {
362e159ab5cSGuenter Roeck 	return (type == hwmon_temp && attr == hwmon_temp_label) ||
363e159ab5cSGuenter Roeck 	       (type == hwmon_in && attr == hwmon_in_label) ||
364e159ab5cSGuenter Roeck 	       (type == hwmon_curr && attr == hwmon_curr_label) ||
365e159ab5cSGuenter Roeck 	       (type == hwmon_power && attr == hwmon_power_label) ||
366e159ab5cSGuenter Roeck 	       (type == hwmon_energy && attr == hwmon_energy_label) ||
367e159ab5cSGuenter Roeck 	       (type == hwmon_humidity && attr == hwmon_humidity_label) ||
368e159ab5cSGuenter Roeck 	       (type == hwmon_fan && attr == hwmon_fan_label);
369e159ab5cSGuenter Roeck }
370e159ab5cSGuenter Roeck 
3713bf8bdcfSGuenter Roeck static struct attribute *hwmon_genattr(const void *drvdata,
372d560168bSGuenter Roeck 				       enum hwmon_sensor_types type,
373d560168bSGuenter Roeck 				       u32 attr,
374d560168bSGuenter Roeck 				       int index,
375d560168bSGuenter Roeck 				       const char *template,
376d560168bSGuenter Roeck 				       const struct hwmon_ops *ops)
377d560168bSGuenter Roeck {
378d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr;
379d560168bSGuenter Roeck 	struct device_attribute *dattr;
380d560168bSGuenter Roeck 	struct attribute *a;
381d560168bSGuenter Roeck 	umode_t mode;
3823b443defSRasmus Villemoes 	const char *name;
383e159ab5cSGuenter Roeck 	bool is_string = is_string_attr(type, attr);
384d560168bSGuenter Roeck 
385d560168bSGuenter Roeck 	/* The attribute is invisible if there is no template string */
386d560168bSGuenter Roeck 	if (!template)
387d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
388d560168bSGuenter Roeck 
389d560168bSGuenter Roeck 	mode = ops->is_visible(drvdata, type, attr, index);
390d560168bSGuenter Roeck 	if (!mode)
391d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
392d560168bSGuenter Roeck 
3930d87116fSGuenter Roeck 	if ((mode & 0444) && ((is_string && !ops->read_string) ||
394e159ab5cSGuenter Roeck 				 (!is_string && !ops->read)))
395d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
3960d87116fSGuenter Roeck 	if ((mode & 0222) && !ops->write)
397d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
398d560168bSGuenter Roeck 
3993bf8bdcfSGuenter Roeck 	hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
400d560168bSGuenter Roeck 	if (!hattr)
401d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
402d560168bSGuenter Roeck 
4033a412d5eSGuenter Roeck 	if (type == hwmon_chip) {
4043b443defSRasmus Villemoes 		name = template;
4053a412d5eSGuenter Roeck 	} else {
4063a412d5eSGuenter Roeck 		scnprintf(hattr->name, sizeof(hattr->name), template,
4073a412d5eSGuenter Roeck 			  index + hwmon_attr_base(type));
4083a412d5eSGuenter Roeck 		name = hattr->name;
4093a412d5eSGuenter Roeck 	}
4103a412d5eSGuenter Roeck 
411d560168bSGuenter Roeck 	hattr->type = type;
412d560168bSGuenter Roeck 	hattr->attr = attr;
413d560168bSGuenter Roeck 	hattr->index = index;
414d560168bSGuenter Roeck 	hattr->ops = ops;
415d560168bSGuenter Roeck 
416d560168bSGuenter Roeck 	dattr = &hattr->dev_attr;
417e159ab5cSGuenter Roeck 	dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
418d560168bSGuenter Roeck 	dattr->store = hwmon_attr_store;
419d560168bSGuenter Roeck 
420d560168bSGuenter Roeck 	a = &dattr->attr;
421d560168bSGuenter Roeck 	sysfs_attr_init(a);
422d560168bSGuenter Roeck 	a->name = name;
423d560168bSGuenter Roeck 	a->mode = mode;
424d560168bSGuenter Roeck 
425d560168bSGuenter Roeck 	return a;
426d560168bSGuenter Roeck }
427d560168bSGuenter Roeck 
428f4d325d5SGuenter Roeck /*
429f4d325d5SGuenter Roeck  * Chip attributes are not attribute templates but actual sysfs attributes.
430f4d325d5SGuenter Roeck  * See hwmon_genattr() for special handling.
431f4d325d5SGuenter Roeck  */
432f4d325d5SGuenter Roeck static const char * const hwmon_chip_attrs[] = {
433d560168bSGuenter Roeck 	[hwmon_chip_temp_reset_history] = "temp_reset_history",
43400d616cfSGuenter Roeck 	[hwmon_chip_in_reset_history] = "in_reset_history",
4359b26947cSGuenter Roeck 	[hwmon_chip_curr_reset_history] = "curr_reset_history",
436b308f5c7SGuenter Roeck 	[hwmon_chip_power_reset_history] = "power_reset_history",
437d560168bSGuenter Roeck 	[hwmon_chip_update_interval] = "update_interval",
438d560168bSGuenter Roeck 	[hwmon_chip_alarms] = "alarms",
4399f00995eSGuenter Roeck 	[hwmon_chip_samples] = "samples",
4409f00995eSGuenter Roeck 	[hwmon_chip_curr_samples] = "curr_samples",
4419f00995eSGuenter Roeck 	[hwmon_chip_in_samples] = "in_samples",
4429f00995eSGuenter Roeck 	[hwmon_chip_power_samples] = "power_samples",
4439f00995eSGuenter Roeck 	[hwmon_chip_temp_samples] = "temp_samples",
444d560168bSGuenter Roeck };
445d560168bSGuenter Roeck 
446d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = {
447002c6b54SGuenter Roeck 	[hwmon_temp_enable] = "temp%d_enable",
448d560168bSGuenter Roeck 	[hwmon_temp_input] = "temp%d_input",
449d560168bSGuenter Roeck 	[hwmon_temp_type] = "temp%d_type",
450d560168bSGuenter Roeck 	[hwmon_temp_lcrit] = "temp%d_lcrit",
451d560168bSGuenter Roeck 	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
452d560168bSGuenter Roeck 	[hwmon_temp_min] = "temp%d_min",
453d560168bSGuenter Roeck 	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
454d560168bSGuenter Roeck 	[hwmon_temp_max] = "temp%d_max",
455d560168bSGuenter Roeck 	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
456d560168bSGuenter Roeck 	[hwmon_temp_crit] = "temp%d_crit",
457d560168bSGuenter Roeck 	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
458d560168bSGuenter Roeck 	[hwmon_temp_emergency] = "temp%d_emergency",
459d560168bSGuenter Roeck 	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
460d560168bSGuenter Roeck 	[hwmon_temp_alarm] = "temp%d_alarm",
461d560168bSGuenter Roeck 	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
462d560168bSGuenter Roeck 	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
463d560168bSGuenter Roeck 	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
464d560168bSGuenter Roeck 	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
465d560168bSGuenter Roeck 	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
466d560168bSGuenter Roeck 	[hwmon_temp_fault] = "temp%d_fault",
467d560168bSGuenter Roeck 	[hwmon_temp_offset] = "temp%d_offset",
468d560168bSGuenter Roeck 	[hwmon_temp_label] = "temp%d_label",
469d560168bSGuenter Roeck 	[hwmon_temp_lowest] = "temp%d_lowest",
470d560168bSGuenter Roeck 	[hwmon_temp_highest] = "temp%d_highest",
471d560168bSGuenter Roeck 	[hwmon_temp_reset_history] = "temp%d_reset_history",
4721967f712SZbigniew Lukwinski 	[hwmon_temp_rated_min] = "temp%d_rated_min",
4731967f712SZbigniew Lukwinski 	[hwmon_temp_rated_max] = "temp%d_rated_max",
474d560168bSGuenter Roeck };
475d560168bSGuenter Roeck 
47600d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = {
477002c6b54SGuenter Roeck 	[hwmon_in_enable] = "in%d_enable",
47800d616cfSGuenter Roeck 	[hwmon_in_input] = "in%d_input",
47900d616cfSGuenter Roeck 	[hwmon_in_min] = "in%d_min",
48000d616cfSGuenter Roeck 	[hwmon_in_max] = "in%d_max",
48100d616cfSGuenter Roeck 	[hwmon_in_lcrit] = "in%d_lcrit",
48200d616cfSGuenter Roeck 	[hwmon_in_crit] = "in%d_crit",
48300d616cfSGuenter Roeck 	[hwmon_in_average] = "in%d_average",
48400d616cfSGuenter Roeck 	[hwmon_in_lowest] = "in%d_lowest",
48500d616cfSGuenter Roeck 	[hwmon_in_highest] = "in%d_highest",
48600d616cfSGuenter Roeck 	[hwmon_in_reset_history] = "in%d_reset_history",
48700d616cfSGuenter Roeck 	[hwmon_in_label] = "in%d_label",
48800d616cfSGuenter Roeck 	[hwmon_in_alarm] = "in%d_alarm",
48900d616cfSGuenter Roeck 	[hwmon_in_min_alarm] = "in%d_min_alarm",
49000d616cfSGuenter Roeck 	[hwmon_in_max_alarm] = "in%d_max_alarm",
49100d616cfSGuenter Roeck 	[hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
49200d616cfSGuenter Roeck 	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
4931967f712SZbigniew Lukwinski 	[hwmon_in_rated_min] = "in%d_rated_min",
4941967f712SZbigniew Lukwinski 	[hwmon_in_rated_max] = "in%d_rated_max",
49500d616cfSGuenter Roeck };
49600d616cfSGuenter Roeck 
4979b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = {
498002c6b54SGuenter Roeck 	[hwmon_curr_enable] = "curr%d_enable",
4999b26947cSGuenter Roeck 	[hwmon_curr_input] = "curr%d_input",
5009b26947cSGuenter Roeck 	[hwmon_curr_min] = "curr%d_min",
5019b26947cSGuenter Roeck 	[hwmon_curr_max] = "curr%d_max",
5029b26947cSGuenter Roeck 	[hwmon_curr_lcrit] = "curr%d_lcrit",
5039b26947cSGuenter Roeck 	[hwmon_curr_crit] = "curr%d_crit",
5049b26947cSGuenter Roeck 	[hwmon_curr_average] = "curr%d_average",
5059b26947cSGuenter Roeck 	[hwmon_curr_lowest] = "curr%d_lowest",
5069b26947cSGuenter Roeck 	[hwmon_curr_highest] = "curr%d_highest",
5079b26947cSGuenter Roeck 	[hwmon_curr_reset_history] = "curr%d_reset_history",
5089b26947cSGuenter Roeck 	[hwmon_curr_label] = "curr%d_label",
5099b26947cSGuenter Roeck 	[hwmon_curr_alarm] = "curr%d_alarm",
5109b26947cSGuenter Roeck 	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
5119b26947cSGuenter Roeck 	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
5129b26947cSGuenter Roeck 	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
5139b26947cSGuenter Roeck 	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
5141967f712SZbigniew Lukwinski 	[hwmon_curr_rated_min] = "curr%d_rated_min",
5151967f712SZbigniew Lukwinski 	[hwmon_curr_rated_max] = "curr%d_rated_max",
5169b26947cSGuenter Roeck };
5179b26947cSGuenter Roeck 
518b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = {
519002c6b54SGuenter Roeck 	[hwmon_power_enable] = "power%d_enable",
520b308f5c7SGuenter Roeck 	[hwmon_power_average] = "power%d_average",
521b308f5c7SGuenter Roeck 	[hwmon_power_average_interval] = "power%d_average_interval",
522b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_max] = "power%d_interval_max",
523b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_min] = "power%d_interval_min",
524b308f5c7SGuenter Roeck 	[hwmon_power_average_highest] = "power%d_average_highest",
525b308f5c7SGuenter Roeck 	[hwmon_power_average_lowest] = "power%d_average_lowest",
526b308f5c7SGuenter Roeck 	[hwmon_power_average_max] = "power%d_average_max",
527b308f5c7SGuenter Roeck 	[hwmon_power_average_min] = "power%d_average_min",
528b308f5c7SGuenter Roeck 	[hwmon_power_input] = "power%d_input",
529b308f5c7SGuenter Roeck 	[hwmon_power_input_highest] = "power%d_input_highest",
530b308f5c7SGuenter Roeck 	[hwmon_power_input_lowest] = "power%d_input_lowest",
531b308f5c7SGuenter Roeck 	[hwmon_power_reset_history] = "power%d_reset_history",
532b308f5c7SGuenter Roeck 	[hwmon_power_accuracy] = "power%d_accuracy",
533b308f5c7SGuenter Roeck 	[hwmon_power_cap] = "power%d_cap",
534b308f5c7SGuenter Roeck 	[hwmon_power_cap_hyst] = "power%d_cap_hyst",
535b308f5c7SGuenter Roeck 	[hwmon_power_cap_max] = "power%d_cap_max",
536b308f5c7SGuenter Roeck 	[hwmon_power_cap_min] = "power%d_cap_min",
537aa7f29b0SAndrew Lunn 	[hwmon_power_min] = "power%d_min",
538b308f5c7SGuenter Roeck 	[hwmon_power_max] = "power%d_max",
539aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit] = "power%d_lcrit",
540b308f5c7SGuenter Roeck 	[hwmon_power_crit] = "power%d_crit",
541b308f5c7SGuenter Roeck 	[hwmon_power_label] = "power%d_label",
542b308f5c7SGuenter Roeck 	[hwmon_power_alarm] = "power%d_alarm",
543b308f5c7SGuenter Roeck 	[hwmon_power_cap_alarm] = "power%d_cap_alarm",
544aa7f29b0SAndrew Lunn 	[hwmon_power_min_alarm] = "power%d_min_alarm",
545b308f5c7SGuenter Roeck 	[hwmon_power_max_alarm] = "power%d_max_alarm",
546aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
547b308f5c7SGuenter Roeck 	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
5481967f712SZbigniew Lukwinski 	[hwmon_power_rated_min] = "power%d_rated_min",
5491967f712SZbigniew Lukwinski 	[hwmon_power_rated_max] = "power%d_rated_max",
550b308f5c7SGuenter Roeck };
551b308f5c7SGuenter Roeck 
5526bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = {
553002c6b54SGuenter Roeck 	[hwmon_energy_enable] = "energy%d_enable",
5546bfcca44SGuenter Roeck 	[hwmon_energy_input] = "energy%d_input",
5556bfcca44SGuenter Roeck 	[hwmon_energy_label] = "energy%d_label",
5566bfcca44SGuenter Roeck };
5576bfcca44SGuenter Roeck 
5586bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = {
559002c6b54SGuenter Roeck 	[hwmon_humidity_enable] = "humidity%d_enable",
5606bfcca44SGuenter Roeck 	[hwmon_humidity_input] = "humidity%d_input",
5616bfcca44SGuenter Roeck 	[hwmon_humidity_label] = "humidity%d_label",
5626bfcca44SGuenter Roeck 	[hwmon_humidity_min] = "humidity%d_min",
5636bfcca44SGuenter Roeck 	[hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
5646bfcca44SGuenter Roeck 	[hwmon_humidity_max] = "humidity%d_max",
5656bfcca44SGuenter Roeck 	[hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
5666bfcca44SGuenter Roeck 	[hwmon_humidity_alarm] = "humidity%d_alarm",
5676bfcca44SGuenter Roeck 	[hwmon_humidity_fault] = "humidity%d_fault",
5681967f712SZbigniew Lukwinski 	[hwmon_humidity_rated_min] = "humidity%d_rated_min",
5691967f712SZbigniew Lukwinski 	[hwmon_humidity_rated_max] = "humidity%d_rated_max",
5706bfcca44SGuenter Roeck };
5716bfcca44SGuenter Roeck 
5728faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = {
573002c6b54SGuenter Roeck 	[hwmon_fan_enable] = "fan%d_enable",
5748faee73fSGuenter Roeck 	[hwmon_fan_input] = "fan%d_input",
5758faee73fSGuenter Roeck 	[hwmon_fan_label] = "fan%d_label",
5768faee73fSGuenter Roeck 	[hwmon_fan_min] = "fan%d_min",
5778faee73fSGuenter Roeck 	[hwmon_fan_max] = "fan%d_max",
5788faee73fSGuenter Roeck 	[hwmon_fan_div] = "fan%d_div",
5798faee73fSGuenter Roeck 	[hwmon_fan_pulses] = "fan%d_pulses",
5808faee73fSGuenter Roeck 	[hwmon_fan_target] = "fan%d_target",
5818faee73fSGuenter Roeck 	[hwmon_fan_alarm] = "fan%d_alarm",
5828faee73fSGuenter Roeck 	[hwmon_fan_min_alarm] = "fan%d_min_alarm",
5838faee73fSGuenter Roeck 	[hwmon_fan_max_alarm] = "fan%d_max_alarm",
5848faee73fSGuenter Roeck 	[hwmon_fan_fault] = "fan%d_fault",
5858faee73fSGuenter Roeck };
5868faee73fSGuenter Roeck 
587f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = {
588f9f7bb3aSGuenter Roeck 	[hwmon_pwm_input] = "pwm%d",
589f9f7bb3aSGuenter Roeck 	[hwmon_pwm_enable] = "pwm%d_enable",
590f9f7bb3aSGuenter Roeck 	[hwmon_pwm_mode] = "pwm%d_mode",
591f9f7bb3aSGuenter Roeck 	[hwmon_pwm_freq] = "pwm%d_freq",
592f9f7bb3aSGuenter Roeck };
593f9f7bb3aSGuenter Roeck 
5944413405fSDr. David Alan Gilbert static const char * const hwmon_intrusion_attr_templates[] = {
5954413405fSDr. David Alan Gilbert 	[hwmon_intrusion_alarm] = "intrusion%d_alarm",
5964413405fSDr. David Alan Gilbert 	[hwmon_intrusion_beep]  = "intrusion%d_beep",
5974413405fSDr. David Alan Gilbert };
5984413405fSDr. David Alan Gilbert 
599d560168bSGuenter Roeck static const char * const *__templates[] = {
600f4d325d5SGuenter Roeck 	[hwmon_chip] = hwmon_chip_attrs,
601d560168bSGuenter Roeck 	[hwmon_temp] = hwmon_temp_attr_templates,
60200d616cfSGuenter Roeck 	[hwmon_in] = hwmon_in_attr_templates,
6039b26947cSGuenter Roeck 	[hwmon_curr] = hwmon_curr_attr_templates,
604b308f5c7SGuenter Roeck 	[hwmon_power] = hwmon_power_attr_templates,
6056bfcca44SGuenter Roeck 	[hwmon_energy] = hwmon_energy_attr_templates,
6066bfcca44SGuenter Roeck 	[hwmon_humidity] = hwmon_humidity_attr_templates,
6078faee73fSGuenter Roeck 	[hwmon_fan] = hwmon_fan_attr_templates,
608f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = hwmon_pwm_attr_templates,
6094413405fSDr. David Alan Gilbert 	[hwmon_intrusion] = hwmon_intrusion_attr_templates,
610d560168bSGuenter Roeck };
611d560168bSGuenter Roeck 
612d560168bSGuenter Roeck static const int __templates_size[] = {
613f4d325d5SGuenter Roeck 	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
614d560168bSGuenter Roeck 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
61500d616cfSGuenter Roeck 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
6169b26947cSGuenter Roeck 	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
617b308f5c7SGuenter Roeck 	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
6186bfcca44SGuenter Roeck 	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
6196bfcca44SGuenter Roeck 	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
6208faee73fSGuenter Roeck 	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
621f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
6224413405fSDr. David Alan Gilbert 	[hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
623d560168bSGuenter Roeck };
624d560168bSGuenter Roeck 
6251597b374SGuenter Roeck int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
6261597b374SGuenter Roeck 		       u32 attr, int channel)
6271597b374SGuenter Roeck {
628*7f3cc8f8SGuenter Roeck 	char event[MAX_SYSFS_ATTR_NAME_LENGTH + 5];
6291597b374SGuenter Roeck 	char sattr[MAX_SYSFS_ATTR_NAME_LENGTH];
630*7f3cc8f8SGuenter Roeck 	char *envp[] = { event, NULL };
6311597b374SGuenter Roeck 	const char * const *templates;
6321597b374SGuenter Roeck 	const char *template;
6331597b374SGuenter Roeck 	int base;
6341597b374SGuenter Roeck 
6351597b374SGuenter Roeck 	if (type >= ARRAY_SIZE(__templates))
6361597b374SGuenter Roeck 		return -EINVAL;
6371597b374SGuenter Roeck 	if (attr >= __templates_size[type])
6381597b374SGuenter Roeck 		return -EINVAL;
6391597b374SGuenter Roeck 
6401597b374SGuenter Roeck 	templates = __templates[type];
6411597b374SGuenter Roeck 	template = templates[attr];
6421597b374SGuenter Roeck 
6431597b374SGuenter Roeck 	base = hwmon_attr_base(type);
6441597b374SGuenter Roeck 
6451597b374SGuenter Roeck 	scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel);
646*7f3cc8f8SGuenter Roeck 	scnprintf(event, sizeof(event), "NAME=%s", sattr);
6471597b374SGuenter Roeck 	sysfs_notify(&dev->kobj, NULL, sattr);
648*7f3cc8f8SGuenter Roeck 	kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
6491597b374SGuenter Roeck 
6501597b374SGuenter Roeck 	if (type == hwmon_temp)
6511597b374SGuenter Roeck 		hwmon_thermal_notify(dev, channel);
6521597b374SGuenter Roeck 
6531597b374SGuenter Roeck 	return 0;
6541597b374SGuenter Roeck }
6551597b374SGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_notify_event);
6561597b374SGuenter Roeck 
657d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
658d560168bSGuenter Roeck {
659d560168bSGuenter Roeck 	int i, n;
660d560168bSGuenter Roeck 
661d560168bSGuenter Roeck 	for (i = n = 0; info->config[i]; i++)
662d560168bSGuenter Roeck 		n += hweight32(info->config[i]);
663d560168bSGuenter Roeck 
664d560168bSGuenter Roeck 	return n;
665d560168bSGuenter Roeck }
666d560168bSGuenter Roeck 
6673bf8bdcfSGuenter Roeck static int hwmon_genattrs(const void *drvdata,
668d560168bSGuenter Roeck 			  struct attribute **attrs,
669d560168bSGuenter Roeck 			  const struct hwmon_ops *ops,
670d560168bSGuenter Roeck 			  const struct hwmon_channel_info *info)
671d560168bSGuenter Roeck {
672d560168bSGuenter Roeck 	const char * const *templates;
673d560168bSGuenter Roeck 	int template_size;
674d560168bSGuenter Roeck 	int i, aindex = 0;
675d560168bSGuenter Roeck 
676d560168bSGuenter Roeck 	if (info->type >= ARRAY_SIZE(__templates))
677d560168bSGuenter Roeck 		return -EINVAL;
678d560168bSGuenter Roeck 
679d560168bSGuenter Roeck 	templates = __templates[info->type];
680d560168bSGuenter Roeck 	template_size = __templates_size[info->type];
681d560168bSGuenter Roeck 
682d560168bSGuenter Roeck 	for (i = 0; info->config[i]; i++) {
683d560168bSGuenter Roeck 		u32 attr_mask = info->config[i];
684d560168bSGuenter Roeck 		u32 attr;
685d560168bSGuenter Roeck 
686d560168bSGuenter Roeck 		while (attr_mask) {
687d560168bSGuenter Roeck 			struct attribute *a;
688d560168bSGuenter Roeck 
689d560168bSGuenter Roeck 			attr = __ffs(attr_mask);
690d560168bSGuenter Roeck 			attr_mask &= ~BIT(attr);
691d560168bSGuenter Roeck 			if (attr >= template_size)
692d560168bSGuenter Roeck 				return -EINVAL;
6933bf8bdcfSGuenter Roeck 			a = hwmon_genattr(drvdata, info->type, attr, i,
694d560168bSGuenter Roeck 					  templates[attr], ops);
695d560168bSGuenter Roeck 			if (IS_ERR(a)) {
696d560168bSGuenter Roeck 				if (PTR_ERR(a) != -ENOENT)
697d560168bSGuenter Roeck 					return PTR_ERR(a);
698d560168bSGuenter Roeck 				continue;
699d560168bSGuenter Roeck 			}
700d560168bSGuenter Roeck 			attrs[aindex++] = a;
701d560168bSGuenter Roeck 		}
702d560168bSGuenter Roeck 	}
703d560168bSGuenter Roeck 	return aindex;
704d560168bSGuenter Roeck }
705d560168bSGuenter Roeck 
706d560168bSGuenter Roeck static struct attribute **
7073bf8bdcfSGuenter Roeck __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
708d560168bSGuenter Roeck {
709d560168bSGuenter Roeck 	int ret, i, aindex = 0, nattrs = 0;
710d560168bSGuenter Roeck 	struct attribute **attrs;
711d560168bSGuenter Roeck 
712d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++)
713d560168bSGuenter Roeck 		nattrs += hwmon_num_channel_attrs(chip->info[i]);
714d560168bSGuenter Roeck 
715d560168bSGuenter Roeck 	if (nattrs == 0)
716d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
717d560168bSGuenter Roeck 
7183bf8bdcfSGuenter Roeck 	attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
719d560168bSGuenter Roeck 	if (!attrs)
720d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
721d560168bSGuenter Roeck 
722d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++) {
7233bf8bdcfSGuenter Roeck 		ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
724d560168bSGuenter Roeck 				     chip->info[i]);
7253bf8bdcfSGuenter Roeck 		if (ret < 0) {
7263bf8bdcfSGuenter Roeck 			hwmon_free_attrs(attrs);
727d560168bSGuenter Roeck 			return ERR_PTR(ret);
7283bf8bdcfSGuenter Roeck 		}
729d560168bSGuenter Roeck 		aindex += ret;
730d560168bSGuenter Roeck 	}
731d560168bSGuenter Roeck 
732d560168bSGuenter Roeck 	return attrs;
733d560168bSGuenter Roeck }
734d560168bSGuenter Roeck 
735d560168bSGuenter Roeck static struct device *
736d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
737d560168bSGuenter Roeck 			const struct hwmon_chip_info *chip,
738d560168bSGuenter Roeck 			const struct attribute_group **groups)
739d560168bSGuenter Roeck {
740d560168bSGuenter Roeck 	struct hwmon_device *hwdev;
741d560168bSGuenter Roeck 	struct device *hdev;
74244e3ad88SAkinobu Mita 	int i, err, id;
743d560168bSGuenter Roeck 
74474d3b641SGuenter Roeck 	/* Complain about invalid characters in hwmon name attribute */
745d560168bSGuenter Roeck 	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
74674d3b641SGuenter Roeck 		dev_warn(dev,
74774d3b641SGuenter Roeck 			 "hwmon: '%s' is not a valid name attribute, please fix\n",
74874d3b641SGuenter Roeck 			 name);
749d560168bSGuenter Roeck 
750d560168bSGuenter Roeck 	id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
751d560168bSGuenter Roeck 	if (id < 0)
752d560168bSGuenter Roeck 		return ERR_PTR(id);
753d560168bSGuenter Roeck 
754d560168bSGuenter Roeck 	hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
755d560168bSGuenter Roeck 	if (hwdev == NULL) {
756d560168bSGuenter Roeck 		err = -ENOMEM;
757d560168bSGuenter Roeck 		goto ida_remove;
758d560168bSGuenter Roeck 	}
759d560168bSGuenter Roeck 
760d560168bSGuenter Roeck 	hdev = &hwdev->dev;
761d560168bSGuenter Roeck 
762239552f4SGuenter Roeck 	if (chip) {
763d560168bSGuenter Roeck 		struct attribute **attrs;
764b2a4cc3aSGuenter Roeck 		int ngroups = 2; /* terminating NULL plus &hwdev->groups */
765d560168bSGuenter Roeck 
766d560168bSGuenter Roeck 		if (groups)
767d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
768d560168bSGuenter Roeck 				ngroups++;
769d560168bSGuenter Roeck 
7703bf8bdcfSGuenter Roeck 		hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
77138d8ed65SColin Ian King 		if (!hwdev->groups) {
77238d8ed65SColin Ian King 			err = -ENOMEM;
77338d8ed65SColin Ian King 			goto free_hwmon;
77438d8ed65SColin Ian King 		}
775d560168bSGuenter Roeck 
7763bf8bdcfSGuenter Roeck 		attrs = __hwmon_create_attrs(drvdata, chip);
777d560168bSGuenter Roeck 		if (IS_ERR(attrs)) {
778d560168bSGuenter Roeck 			err = PTR_ERR(attrs);
779d560168bSGuenter Roeck 			goto free_hwmon;
780d560168bSGuenter Roeck 		}
781d560168bSGuenter Roeck 
782d560168bSGuenter Roeck 		hwdev->group.attrs = attrs;
783d560168bSGuenter Roeck 		ngroups = 0;
784d560168bSGuenter Roeck 		hwdev->groups[ngroups++] = &hwdev->group;
785d560168bSGuenter Roeck 
786d560168bSGuenter Roeck 		if (groups) {
787d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
788d560168bSGuenter Roeck 				hwdev->groups[ngroups++] = groups[i];
789d560168bSGuenter Roeck 		}
790d560168bSGuenter Roeck 
791d560168bSGuenter Roeck 		hdev->groups = hwdev->groups;
792d560168bSGuenter Roeck 	} else {
793d560168bSGuenter Roeck 		hdev->groups = groups;
794d560168bSGuenter Roeck 	}
795d560168bSGuenter Roeck 
796d560168bSGuenter Roeck 	hwdev->name = name;
797d560168bSGuenter Roeck 	hdev->class = &hwmon_class;
798d560168bSGuenter Roeck 	hdev->parent = dev;
799d560168bSGuenter Roeck 	hdev->of_node = dev ? dev->of_node : NULL;
800d560168bSGuenter Roeck 	hwdev->chip = chip;
801d560168bSGuenter Roeck 	dev_set_drvdata(hdev, drvdata);
802d560168bSGuenter Roeck 	dev_set_name(hdev, HWMON_ID_FORMAT, id);
803d560168bSGuenter Roeck 	err = device_register(hdev);
804ada61aa0SYang Yingliang 	if (err) {
805ada61aa0SYang Yingliang 		put_device(hdev);
806ada61aa0SYang Yingliang 		goto ida_remove;
807ada61aa0SYang Yingliang 	}
808d560168bSGuenter Roeck 
8091597b374SGuenter Roeck 	INIT_LIST_HEAD(&hwdev->tzdata);
8101597b374SGuenter Roeck 
811c41dd48eSEduardo Valentin 	if (dev && dev->of_node && chip && chip->ops->read &&
812d560168bSGuenter Roeck 	    chip->info[0]->type == hwmon_chip &&
813d560168bSGuenter Roeck 	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
81444e3ad88SAkinobu Mita 		err = hwmon_thermal_register_sensors(hdev);
81574e35127SDmitry Osipenko 		if (err) {
81674e35127SDmitry Osipenko 			device_unregister(hdev);
817792eac18SGuenter Roeck 			/*
81844e3ad88SAkinobu Mita 			 * Don't worry about hwdev; hwmon_dev_release(), called
81944e3ad88SAkinobu Mita 			 * from device_unregister(), will free it.
820792eac18SGuenter Roeck 			 */
82174e35127SDmitry Osipenko 			goto ida_remove;
82274e35127SDmitry Osipenko 		}
82347c332deSLinus Walleij 	}
824d560168bSGuenter Roeck 
825d560168bSGuenter Roeck 	return hdev;
826d560168bSGuenter Roeck 
827d560168bSGuenter Roeck free_hwmon:
8283bf8bdcfSGuenter Roeck 	hwmon_dev_release(hdev);
829d560168bSGuenter Roeck ida_remove:
830d560168bSGuenter Roeck 	ida_simple_remove(&hwmon_ida, id);
831d560168bSGuenter Roeck 	return ERR_PTR(err);
832d560168bSGuenter Roeck }
833d560168bSGuenter Roeck 
8341236441fSMark M. Hoffman /**
835bab2243cSGuenter Roeck  * hwmon_device_register_with_groups - register w/ hwmon
836bab2243cSGuenter Roeck  * @dev: the parent device
837bab2243cSGuenter Roeck  * @name: hwmon name attribute
838bab2243cSGuenter Roeck  * @drvdata: driver data to attach to created device
839bab2243cSGuenter Roeck  * @groups: List of attribute groups to create
840bab2243cSGuenter Roeck  *
841bab2243cSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
842bab2243cSGuenter Roeck  * longer needed.
843bab2243cSGuenter Roeck  *
844bab2243cSGuenter Roeck  * Returns the pointer to the new device.
845bab2243cSGuenter Roeck  */
846bab2243cSGuenter Roeck struct device *
847bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name,
848bab2243cSGuenter Roeck 				  void *drvdata,
849bab2243cSGuenter Roeck 				  const struct attribute_group **groups)
850bab2243cSGuenter Roeck {
8518353863aSGuenter Roeck 	if (!name)
8528353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
8538353863aSGuenter Roeck 
854d560168bSGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
855bab2243cSGuenter Roeck }
856bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
857bab2243cSGuenter Roeck 
858bab2243cSGuenter Roeck /**
859d560168bSGuenter Roeck  * hwmon_device_register_with_info - register w/ hwmon
860d560168bSGuenter Roeck  * @dev: the parent device
861d560168bSGuenter Roeck  * @name: hwmon name attribute
862d560168bSGuenter Roeck  * @drvdata: driver data to attach to created device
8633870945aSGuenter Roeck  * @chip: pointer to hwmon chip information
864848ba0a2SGuenter Roeck  * @extra_groups: pointer to list of additional non-standard attribute groups
865d560168bSGuenter Roeck  *
866d560168bSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
867d560168bSGuenter Roeck  * longer needed.
868d560168bSGuenter Roeck  *
869d560168bSGuenter Roeck  * Returns the pointer to the new device.
870d560168bSGuenter Roeck  */
871d560168bSGuenter Roeck struct device *
872d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name,
873d560168bSGuenter Roeck 				void *drvdata,
874d560168bSGuenter Roeck 				const struct hwmon_chip_info *chip,
875848ba0a2SGuenter Roeck 				const struct attribute_group **extra_groups)
876d560168bSGuenter Roeck {
8778353863aSGuenter Roeck 	if (!name)
8788353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
8798353863aSGuenter Roeck 
880239552f4SGuenter Roeck 	if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
881d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
882d560168bSGuenter Roeck 
88359df4f4eSLucas Magasweran 	if (chip && !dev)
88459df4f4eSLucas Magasweran 		return ERR_PTR(-EINVAL);
88559df4f4eSLucas Magasweran 
886848ba0a2SGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
887d560168bSGuenter Roeck }
888d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
889d560168bSGuenter Roeck 
890d560168bSGuenter Roeck /**
8911beeffe4STony Jones  * hwmon_device_register - register w/ hwmon
8921236441fSMark M. Hoffman  * @dev: the device to register
8931236441fSMark M. Hoffman  *
8941beeffe4STony Jones  * hwmon_device_unregister() must be called when the device is no
8951236441fSMark M. Hoffman  * longer needed.
8961236441fSMark M. Hoffman  *
8971beeffe4STony Jones  * Returns the pointer to the new device.
8981236441fSMark M. Hoffman  */
8991beeffe4STony Jones struct device *hwmon_device_register(struct device *dev)
9001236441fSMark M. Hoffman {
901af1bd36cSGuenter Roeck 	dev_warn(dev,
902af1bd36cSGuenter Roeck 		 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
903af1bd36cSGuenter Roeck 
9048353863aSGuenter Roeck 	return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
9051236441fSMark M. Hoffman }
906839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register);
9071236441fSMark M. Hoffman 
9081236441fSMark M. Hoffman /**
9091236441fSMark M. Hoffman  * hwmon_device_unregister - removes the previously registered class device
9101236441fSMark M. Hoffman  *
9111beeffe4STony Jones  * @dev: the class device to destroy
9121236441fSMark M. Hoffman  */
9131beeffe4STony Jones void hwmon_device_unregister(struct device *dev)
9141236441fSMark M. Hoffman {
9151236441fSMark M. Hoffman 	int id;
9161236441fSMark M. Hoffman 
917739cf3a2SKay Sievers 	if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
9181beeffe4STony Jones 		device_unregister(dev);
9194ca5f468SJonathan Cameron 		ida_simple_remove(&hwmon_ida, id);
9201236441fSMark M. Hoffman 	} else
9211beeffe4STony Jones 		dev_dbg(dev->parent,
9221236441fSMark M. Hoffman 			"hwmon_device_unregister() failed: bad class ID!\n");
9231236441fSMark M. Hoffman }
924839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister);
9251236441fSMark M. Hoffman 
92674188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res)
92774188cbaSGuenter Roeck {
92874188cbaSGuenter Roeck 	struct device *hwdev = *(struct device **)res;
92974188cbaSGuenter Roeck 
93074188cbaSGuenter Roeck 	hwmon_device_unregister(hwdev);
93174188cbaSGuenter Roeck }
93274188cbaSGuenter Roeck 
93374188cbaSGuenter Roeck /**
93474188cbaSGuenter Roeck  * devm_hwmon_device_register_with_groups - register w/ hwmon
93574188cbaSGuenter Roeck  * @dev: the parent device
93674188cbaSGuenter Roeck  * @name: hwmon name attribute
93774188cbaSGuenter Roeck  * @drvdata: driver data to attach to created device
93874188cbaSGuenter Roeck  * @groups: List of attribute groups to create
93974188cbaSGuenter Roeck  *
94074188cbaSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
94174188cbaSGuenter Roeck  * unregistered with the parent device.
94274188cbaSGuenter Roeck  */
94374188cbaSGuenter Roeck struct device *
94474188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
94574188cbaSGuenter Roeck 				       void *drvdata,
94674188cbaSGuenter Roeck 				       const struct attribute_group **groups)
94774188cbaSGuenter Roeck {
94874188cbaSGuenter Roeck 	struct device **ptr, *hwdev;
94974188cbaSGuenter Roeck 
95074188cbaSGuenter Roeck 	if (!dev)
95174188cbaSGuenter Roeck 		return ERR_PTR(-EINVAL);
95274188cbaSGuenter Roeck 
95374188cbaSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
95474188cbaSGuenter Roeck 	if (!ptr)
95574188cbaSGuenter Roeck 		return ERR_PTR(-ENOMEM);
95674188cbaSGuenter Roeck 
95774188cbaSGuenter Roeck 	hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
95874188cbaSGuenter Roeck 	if (IS_ERR(hwdev))
95974188cbaSGuenter Roeck 		goto error;
96074188cbaSGuenter Roeck 
96174188cbaSGuenter Roeck 	*ptr = hwdev;
96274188cbaSGuenter Roeck 	devres_add(dev, ptr);
96374188cbaSGuenter Roeck 	return hwdev;
96474188cbaSGuenter Roeck 
96574188cbaSGuenter Roeck error:
96674188cbaSGuenter Roeck 	devres_free(ptr);
96774188cbaSGuenter Roeck 	return hwdev;
96874188cbaSGuenter Roeck }
96974188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
97074188cbaSGuenter Roeck 
971d560168bSGuenter Roeck /**
972d560168bSGuenter Roeck  * devm_hwmon_device_register_with_info - register w/ hwmon
973d560168bSGuenter Roeck  * @dev:	the parent device
974d560168bSGuenter Roeck  * @name:	hwmon name attribute
975d560168bSGuenter Roeck  * @drvdata:	driver data to attach to created device
9763870945aSGuenter Roeck  * @chip:	pointer to hwmon chip information
9773870945aSGuenter Roeck  * @groups:	pointer to list of driver specific attribute groups
978d560168bSGuenter Roeck  *
979d560168bSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
980d560168bSGuenter Roeck  * unregistered with the parent device.
981d560168bSGuenter Roeck  */
982d560168bSGuenter Roeck struct device *
983d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name,
984d560168bSGuenter Roeck 				     void *drvdata,
985d560168bSGuenter Roeck 				     const struct hwmon_chip_info *chip,
986d560168bSGuenter Roeck 				     const struct attribute_group **groups)
987d560168bSGuenter Roeck {
988d560168bSGuenter Roeck 	struct device **ptr, *hwdev;
989d560168bSGuenter Roeck 
990d560168bSGuenter Roeck 	if (!dev)
991d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
992d560168bSGuenter Roeck 
993d560168bSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
994d560168bSGuenter Roeck 	if (!ptr)
995d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
996d560168bSGuenter Roeck 
997d560168bSGuenter Roeck 	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
998d560168bSGuenter Roeck 						groups);
999d560168bSGuenter Roeck 	if (IS_ERR(hwdev))
1000d560168bSGuenter Roeck 		goto error;
1001d560168bSGuenter Roeck 
1002d560168bSGuenter Roeck 	*ptr = hwdev;
1003d560168bSGuenter Roeck 	devres_add(dev, ptr);
1004d560168bSGuenter Roeck 
1005d560168bSGuenter Roeck 	return hwdev;
1006d560168bSGuenter Roeck 
1007d560168bSGuenter Roeck error:
1008d560168bSGuenter Roeck 	devres_free(ptr);
1009d560168bSGuenter Roeck 	return hwdev;
1010d560168bSGuenter Roeck }
1011d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
1012d560168bSGuenter Roeck 
101374188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data)
101474188cbaSGuenter Roeck {
101574188cbaSGuenter Roeck 	struct device **hwdev = res;
101674188cbaSGuenter Roeck 
101774188cbaSGuenter Roeck 	return *hwdev == data;
101874188cbaSGuenter Roeck }
101974188cbaSGuenter Roeck 
102074188cbaSGuenter Roeck /**
102174188cbaSGuenter Roeck  * devm_hwmon_device_unregister - removes a previously registered hwmon device
102274188cbaSGuenter Roeck  *
102374188cbaSGuenter Roeck  * @dev: the parent device of the device to unregister
102474188cbaSGuenter Roeck  */
102574188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev)
102674188cbaSGuenter Roeck {
102774188cbaSGuenter Roeck 	WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
102874188cbaSGuenter Roeck }
102974188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
103074188cbaSGuenter Roeck 
10312958b1ecSJean Delvare static void __init hwmon_pci_quirks(void)
10322958b1ecSJean Delvare {
10332958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI
10342958b1ecSJean Delvare 	struct pci_dev *sb;
10352958b1ecSJean Delvare 	u16 base;
10362958b1ecSJean Delvare 	u8 enable;
10372958b1ecSJean Delvare 
10382958b1ecSJean Delvare 	/* Open access to 0x295-0x296 on MSI MS-7031 */
10392958b1ecSJean Delvare 	sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
1040d6dab7ddSJean Delvare 	if (sb) {
1041d6dab7ddSJean Delvare 		if (sb->subsystem_vendor == 0x1462 &&	/* MSI */
1042d6dab7ddSJean Delvare 		    sb->subsystem_device == 0x0031) {	/* MS-7031 */
10432958b1ecSJean Delvare 			pci_read_config_byte(sb, 0x48, &enable);
10442958b1ecSJean Delvare 			pci_read_config_word(sb, 0x64, &base);
10452958b1ecSJean Delvare 
10462958b1ecSJean Delvare 			if (base == 0 && !(enable & BIT(2))) {
10472958b1ecSJean Delvare 				dev_info(&sb->dev,
10482958b1ecSJean Delvare 					 "Opening wide generic port at 0x295\n");
10492958b1ecSJean Delvare 				pci_write_config_word(sb, 0x64, 0x295);
1050d6dab7ddSJean Delvare 				pci_write_config_byte(sb, 0x48,
1051d6dab7ddSJean Delvare 						      enable | BIT(2));
10522958b1ecSJean Delvare 			}
10532958b1ecSJean Delvare 		}
1054d6dab7ddSJean Delvare 		pci_dev_put(sb);
1055d6dab7ddSJean Delvare 	}
10562958b1ecSJean Delvare #endif
10572958b1ecSJean Delvare }
10582958b1ecSJean Delvare 
10591236441fSMark M. Hoffman static int __init hwmon_init(void)
10601236441fSMark M. Hoffman {
1061bab2243cSGuenter Roeck 	int err;
1062bab2243cSGuenter Roeck 
10632958b1ecSJean Delvare 	hwmon_pci_quirks();
10642958b1ecSJean Delvare 
1065bab2243cSGuenter Roeck 	err = class_register(&hwmon_class);
1066bab2243cSGuenter Roeck 	if (err) {
1067bab2243cSGuenter Roeck 		pr_err("couldn't register hwmon sysfs class\n");
1068bab2243cSGuenter Roeck 		return err;
10691236441fSMark M. Hoffman 	}
10701236441fSMark M. Hoffman 	return 0;
10711236441fSMark M. Hoffman }
10721236441fSMark M. Hoffman 
10731236441fSMark M. Hoffman static void __exit hwmon_exit(void)
10741236441fSMark M. Hoffman {
1075bab2243cSGuenter Roeck 	class_unregister(&hwmon_class);
10761236441fSMark M. Hoffman }
10771236441fSMark M. Hoffman 
107837f54ee5SDavid Brownell subsys_initcall(hwmon_init);
10791236441fSMark M. Hoffman module_exit(hwmon_exit);
10801236441fSMark M. Hoffman 
10811236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
10821236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
10831236441fSMark M. Hoffman MODULE_LICENSE("GPL");
10841236441fSMark M. Hoffman 
1085