xref: /linux/drivers/hwmon/hwmon.c (revision 002c6b545b85676539add33add8aa7f1f49cbfff)
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>
18c9ebbe6fSGuenter Roeck #include <linux/module.h>
192958b1ecSJean Delvare #include <linux/pci.h>
20c9ebbe6fSGuenter Roeck #include <linux/slab.h>
21648cd48cSGuenter Roeck #include <linux/string.h>
22d560168bSGuenter Roeck #include <linux/thermal.h>
231236441fSMark M. Hoffman 
2461b8ab2cSNicolin Chen #define CREATE_TRACE_POINTS
2561b8ab2cSNicolin Chen #include <trace/events/hwmon.h>
2661b8ab2cSNicolin Chen 
271236441fSMark M. Hoffman #define HWMON_ID_PREFIX "hwmon"
281236441fSMark M. Hoffman #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
291236441fSMark M. Hoffman 
30bab2243cSGuenter Roeck struct hwmon_device {
31bab2243cSGuenter Roeck 	const char *name;
32bab2243cSGuenter Roeck 	struct device dev;
33d560168bSGuenter Roeck 	const struct hwmon_chip_info *chip;
34d560168bSGuenter Roeck 
35d560168bSGuenter Roeck 	struct attribute_group group;
36d560168bSGuenter Roeck 	const struct attribute_group **groups;
37bab2243cSGuenter Roeck };
38d560168bSGuenter Roeck 
39bab2243cSGuenter Roeck #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
40bab2243cSGuenter Roeck 
413a412d5eSGuenter Roeck #define MAX_SYSFS_ATTR_NAME_LENGTH	32
423a412d5eSGuenter Roeck 
43d560168bSGuenter Roeck struct hwmon_device_attribute {
44d560168bSGuenter Roeck 	struct device_attribute dev_attr;
45d560168bSGuenter Roeck 	const struct hwmon_ops *ops;
46d560168bSGuenter Roeck 	enum hwmon_sensor_types type;
47d560168bSGuenter Roeck 	u32 attr;
48d560168bSGuenter Roeck 	int index;
493a412d5eSGuenter Roeck 	char name[MAX_SYSFS_ATTR_NAME_LENGTH];
50d560168bSGuenter Roeck };
51d560168bSGuenter Roeck 
52d560168bSGuenter Roeck #define to_hwmon_attr(d) \
53d560168bSGuenter Roeck 	container_of(d, struct hwmon_device_attribute, dev_attr)
543bf8bdcfSGuenter Roeck #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
55d560168bSGuenter Roeck 
56d560168bSGuenter Roeck /*
57d560168bSGuenter Roeck  * Thermal zone information
58d560168bSGuenter Roeck  * In addition to the reference to the hwmon device,
59d560168bSGuenter Roeck  * also provides the sensor index.
60d560168bSGuenter Roeck  */
61d560168bSGuenter Roeck struct hwmon_thermal_data {
623bf8bdcfSGuenter Roeck 	struct device *dev;		/* Reference to hwmon device */
63d560168bSGuenter Roeck 	int index;			/* sensor index */
64d560168bSGuenter Roeck };
65d560168bSGuenter Roeck 
66bab2243cSGuenter Roeck static ssize_t
672ab0c6c5SJulia Lawall name_show(struct device *dev, struct device_attribute *attr, char *buf)
68bab2243cSGuenter Roeck {
69bab2243cSGuenter Roeck 	return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
70bab2243cSGuenter Roeck }
712ab0c6c5SJulia Lawall static DEVICE_ATTR_RO(name);
72bab2243cSGuenter Roeck 
73bab2243cSGuenter Roeck static struct attribute *hwmon_dev_attrs[] = {
74bab2243cSGuenter Roeck 	&dev_attr_name.attr,
75bab2243cSGuenter Roeck 	NULL
76bab2243cSGuenter Roeck };
77bab2243cSGuenter Roeck 
78bab2243cSGuenter Roeck static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
79bab2243cSGuenter Roeck 					 struct attribute *attr, int n)
80bab2243cSGuenter Roeck {
81bab2243cSGuenter Roeck 	struct device *dev = container_of(kobj, struct device, kobj);
82bab2243cSGuenter Roeck 
83bab2243cSGuenter Roeck 	if (to_hwmon_device(dev)->name == NULL)
84bab2243cSGuenter Roeck 		return 0;
85bab2243cSGuenter Roeck 
86bab2243cSGuenter Roeck 	return attr->mode;
87bab2243cSGuenter Roeck }
88bab2243cSGuenter Roeck 
89524703acSArvind Yadav static const struct attribute_group hwmon_dev_attr_group = {
90bab2243cSGuenter Roeck 	.attrs		= hwmon_dev_attrs,
91bab2243cSGuenter Roeck 	.is_visible	= hwmon_dev_name_is_visible,
92bab2243cSGuenter Roeck };
93bab2243cSGuenter Roeck 
94bab2243cSGuenter Roeck static const struct attribute_group *hwmon_dev_attr_groups[] = {
95bab2243cSGuenter Roeck 	&hwmon_dev_attr_group,
96bab2243cSGuenter Roeck 	NULL
97bab2243cSGuenter Roeck };
98bab2243cSGuenter Roeck 
993bf8bdcfSGuenter Roeck static void hwmon_free_attrs(struct attribute **attrs)
1003bf8bdcfSGuenter Roeck {
1013bf8bdcfSGuenter Roeck 	int i;
1023bf8bdcfSGuenter Roeck 
1033bf8bdcfSGuenter Roeck 	for (i = 0; attrs[i]; i++) {
1043bf8bdcfSGuenter Roeck 		struct device_attribute *dattr = to_dev_attr(attrs[i]);
1053bf8bdcfSGuenter Roeck 		struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr);
1063bf8bdcfSGuenter Roeck 
1073bf8bdcfSGuenter Roeck 		kfree(hattr);
1083bf8bdcfSGuenter Roeck 	}
1093bf8bdcfSGuenter Roeck 	kfree(attrs);
1103bf8bdcfSGuenter Roeck }
1113bf8bdcfSGuenter Roeck 
112bab2243cSGuenter Roeck static void hwmon_dev_release(struct device *dev)
113bab2243cSGuenter Roeck {
1143bf8bdcfSGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(dev);
1153bf8bdcfSGuenter Roeck 
1163bf8bdcfSGuenter Roeck 	if (hwdev->group.attrs)
1173bf8bdcfSGuenter Roeck 		hwmon_free_attrs(hwdev->group.attrs);
1183bf8bdcfSGuenter Roeck 	kfree(hwdev->groups);
1193bf8bdcfSGuenter Roeck 	kfree(hwdev);
120bab2243cSGuenter Roeck }
121bab2243cSGuenter Roeck 
122bab2243cSGuenter Roeck static struct class hwmon_class = {
123bab2243cSGuenter Roeck 	.name = "hwmon",
124bab2243cSGuenter Roeck 	.owner = THIS_MODULE,
125bab2243cSGuenter Roeck 	.dev_groups = hwmon_dev_attr_groups,
126bab2243cSGuenter Roeck 	.dev_release = hwmon_dev_release,
127bab2243cSGuenter Roeck };
1281236441fSMark M. Hoffman 
1294ca5f468SJonathan Cameron static DEFINE_IDA(hwmon_ida);
1301236441fSMark M. Hoffman 
131d560168bSGuenter Roeck /* Thermal zone handling */
132d560168bSGuenter Roeck 
13386430c1aSGuenter Roeck /*
13486430c1aSGuenter Roeck  * The complex conditional is necessary to avoid a cyclic dependency
13586430c1aSGuenter Roeck  * between hwmon and thermal_sys modules.
13686430c1aSGuenter Roeck  */
137f3735332SDaniel Lezcano #ifdef CONFIG_THERMAL_OF
138d560168bSGuenter Roeck static int hwmon_thermal_get_temp(void *data, int *temp)
139d560168bSGuenter Roeck {
140d560168bSGuenter Roeck 	struct hwmon_thermal_data *tdata = data;
1413bf8bdcfSGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
142d560168bSGuenter Roeck 	int ret;
143d560168bSGuenter Roeck 	long t;
144d560168bSGuenter Roeck 
1453bf8bdcfSGuenter Roeck 	ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
146d560168bSGuenter Roeck 				     tdata->index, &t);
147d560168bSGuenter Roeck 	if (ret < 0)
148d560168bSGuenter Roeck 		return ret;
149d560168bSGuenter Roeck 
150d560168bSGuenter Roeck 	*temp = t;
151d560168bSGuenter Roeck 
152d560168bSGuenter Roeck 	return 0;
153d560168bSGuenter Roeck }
154d560168bSGuenter Roeck 
155c9920650SJulia Lawall static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
156d560168bSGuenter Roeck 	.get_temp = hwmon_thermal_get_temp,
157d560168bSGuenter Roeck };
158d560168bSGuenter Roeck 
1593bf8bdcfSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, int index)
160d560168bSGuenter Roeck {
161d560168bSGuenter Roeck 	struct hwmon_thermal_data *tdata;
16247c332deSLinus Walleij 	struct thermal_zone_device *tzd;
163d560168bSGuenter Roeck 
164d560168bSGuenter Roeck 	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
165d560168bSGuenter Roeck 	if (!tdata)
166d560168bSGuenter Roeck 		return -ENOMEM;
167d560168bSGuenter Roeck 
1683bf8bdcfSGuenter Roeck 	tdata->dev = dev;
169d560168bSGuenter Roeck 	tdata->index = index;
170d560168bSGuenter Roeck 
1713bf8bdcfSGuenter Roeck 	tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata,
172d560168bSGuenter Roeck 						   &hwmon_thermal_ops);
17347c332deSLinus Walleij 	/*
17447c332deSLinus Walleij 	 * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV,
17547c332deSLinus Walleij 	 * so ignore that error but forward any other error.
17647c332deSLinus Walleij 	 */
17747c332deSLinus Walleij 	if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV))
17847c332deSLinus Walleij 		return PTR_ERR(tzd);
179d560168bSGuenter Roeck 
180d560168bSGuenter Roeck 	return 0;
181d560168bSGuenter Roeck }
182d560168bSGuenter Roeck #else
1833bf8bdcfSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, int index)
184d560168bSGuenter Roeck {
185d560168bSGuenter Roeck 	return 0;
186d560168bSGuenter Roeck }
18786430c1aSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
188d560168bSGuenter Roeck 
18961b8ab2cSNicolin Chen static int hwmon_attr_base(enum hwmon_sensor_types type)
19061b8ab2cSNicolin Chen {
1914413405fSDr. David Alan Gilbert 	if (type == hwmon_in || type == hwmon_intrusion)
19261b8ab2cSNicolin Chen 		return 0;
19361b8ab2cSNicolin Chen 	return 1;
19461b8ab2cSNicolin Chen }
19561b8ab2cSNicolin Chen 
196d560168bSGuenter Roeck /* sysfs attribute management */
197d560168bSGuenter Roeck 
198d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev,
199d560168bSGuenter Roeck 			       struct device_attribute *devattr, char *buf)
200d560168bSGuenter Roeck {
201d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
202d560168bSGuenter Roeck 	long val;
203d560168bSGuenter Roeck 	int ret;
204d560168bSGuenter Roeck 
205d560168bSGuenter Roeck 	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
206d560168bSGuenter Roeck 			       &val);
207d560168bSGuenter Roeck 	if (ret < 0)
208d560168bSGuenter Roeck 		return ret;
209d560168bSGuenter Roeck 
21061b8ab2cSNicolin Chen 	trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
21161b8ab2cSNicolin Chen 			      hattr->name, val);
21261b8ab2cSNicolin Chen 
213d560168bSGuenter Roeck 	return sprintf(buf, "%ld\n", val);
214d560168bSGuenter Roeck }
215d560168bSGuenter Roeck 
216e159ab5cSGuenter Roeck static ssize_t hwmon_attr_show_string(struct device *dev,
217e159ab5cSGuenter Roeck 				      struct device_attribute *devattr,
218e159ab5cSGuenter Roeck 				      char *buf)
219e159ab5cSGuenter Roeck {
220e159ab5cSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
22161b8ab2cSNicolin Chen 	enum hwmon_sensor_types type = hattr->type;
2225ba6bcbcSJean Delvare 	const char *s;
223e159ab5cSGuenter Roeck 	int ret;
224e159ab5cSGuenter Roeck 
225e159ab5cSGuenter Roeck 	ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
226e159ab5cSGuenter Roeck 				      hattr->index, &s);
227e159ab5cSGuenter Roeck 	if (ret < 0)
228e159ab5cSGuenter Roeck 		return ret;
229e159ab5cSGuenter Roeck 
23061b8ab2cSNicolin Chen 	trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
23161b8ab2cSNicolin Chen 				     hattr->name, s);
23261b8ab2cSNicolin Chen 
233e159ab5cSGuenter Roeck 	return sprintf(buf, "%s\n", s);
234e159ab5cSGuenter Roeck }
235e159ab5cSGuenter Roeck 
236d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev,
237d560168bSGuenter Roeck 				struct device_attribute *devattr,
238d560168bSGuenter Roeck 				const char *buf, size_t count)
239d560168bSGuenter Roeck {
240d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
241d560168bSGuenter Roeck 	long val;
242d560168bSGuenter Roeck 	int ret;
243d560168bSGuenter Roeck 
244d560168bSGuenter Roeck 	ret = kstrtol(buf, 10, &val);
245d560168bSGuenter Roeck 	if (ret < 0)
246d560168bSGuenter Roeck 		return ret;
247d560168bSGuenter Roeck 
248d560168bSGuenter Roeck 	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
249d560168bSGuenter Roeck 				val);
250d560168bSGuenter Roeck 	if (ret < 0)
251d560168bSGuenter Roeck 		return ret;
252d560168bSGuenter Roeck 
25361b8ab2cSNicolin Chen 	trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
25461b8ab2cSNicolin Chen 			       hattr->name, val);
255d560168bSGuenter Roeck 
25661b8ab2cSNicolin Chen 	return count;
257d560168bSGuenter Roeck }
258d560168bSGuenter Roeck 
259e159ab5cSGuenter Roeck static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
260e159ab5cSGuenter Roeck {
261e159ab5cSGuenter Roeck 	return (type == hwmon_temp && attr == hwmon_temp_label) ||
262e159ab5cSGuenter Roeck 	       (type == hwmon_in && attr == hwmon_in_label) ||
263e159ab5cSGuenter Roeck 	       (type == hwmon_curr && attr == hwmon_curr_label) ||
264e159ab5cSGuenter Roeck 	       (type == hwmon_power && attr == hwmon_power_label) ||
265e159ab5cSGuenter Roeck 	       (type == hwmon_energy && attr == hwmon_energy_label) ||
266e159ab5cSGuenter Roeck 	       (type == hwmon_humidity && attr == hwmon_humidity_label) ||
267e159ab5cSGuenter Roeck 	       (type == hwmon_fan && attr == hwmon_fan_label);
268e159ab5cSGuenter Roeck }
269e159ab5cSGuenter Roeck 
2703bf8bdcfSGuenter Roeck static struct attribute *hwmon_genattr(const void *drvdata,
271d560168bSGuenter Roeck 				       enum hwmon_sensor_types type,
272d560168bSGuenter Roeck 				       u32 attr,
273d560168bSGuenter Roeck 				       int index,
274d560168bSGuenter Roeck 				       const char *template,
275d560168bSGuenter Roeck 				       const struct hwmon_ops *ops)
276d560168bSGuenter Roeck {
277d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr;
278d560168bSGuenter Roeck 	struct device_attribute *dattr;
279d560168bSGuenter Roeck 	struct attribute *a;
280d560168bSGuenter Roeck 	umode_t mode;
2813b443defSRasmus Villemoes 	const char *name;
282e159ab5cSGuenter Roeck 	bool is_string = is_string_attr(type, attr);
283d560168bSGuenter Roeck 
284d560168bSGuenter Roeck 	/* The attribute is invisible if there is no template string */
285d560168bSGuenter Roeck 	if (!template)
286d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
287d560168bSGuenter Roeck 
288d560168bSGuenter Roeck 	mode = ops->is_visible(drvdata, type, attr, index);
289d560168bSGuenter Roeck 	if (!mode)
290d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
291d560168bSGuenter Roeck 
2920d87116fSGuenter Roeck 	if ((mode & 0444) && ((is_string && !ops->read_string) ||
293e159ab5cSGuenter Roeck 				 (!is_string && !ops->read)))
294d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
2950d87116fSGuenter Roeck 	if ((mode & 0222) && !ops->write)
296d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
297d560168bSGuenter Roeck 
2983bf8bdcfSGuenter Roeck 	hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
299d560168bSGuenter Roeck 	if (!hattr)
300d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
301d560168bSGuenter Roeck 
3023a412d5eSGuenter Roeck 	if (type == hwmon_chip) {
3033b443defSRasmus Villemoes 		name = template;
3043a412d5eSGuenter Roeck 	} else {
3053a412d5eSGuenter Roeck 		scnprintf(hattr->name, sizeof(hattr->name), template,
3063a412d5eSGuenter Roeck 			  index + hwmon_attr_base(type));
3073a412d5eSGuenter Roeck 		name = hattr->name;
3083a412d5eSGuenter Roeck 	}
3093a412d5eSGuenter Roeck 
310d560168bSGuenter Roeck 	hattr->type = type;
311d560168bSGuenter Roeck 	hattr->attr = attr;
312d560168bSGuenter Roeck 	hattr->index = index;
313d560168bSGuenter Roeck 	hattr->ops = ops;
314d560168bSGuenter Roeck 
315d560168bSGuenter Roeck 	dattr = &hattr->dev_attr;
316e159ab5cSGuenter Roeck 	dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
317d560168bSGuenter Roeck 	dattr->store = hwmon_attr_store;
318d560168bSGuenter Roeck 
319d560168bSGuenter Roeck 	a = &dattr->attr;
320d560168bSGuenter Roeck 	sysfs_attr_init(a);
321d560168bSGuenter Roeck 	a->name = name;
322d560168bSGuenter Roeck 	a->mode = mode;
323d560168bSGuenter Roeck 
324d560168bSGuenter Roeck 	return a;
325d560168bSGuenter Roeck }
326d560168bSGuenter Roeck 
327f4d325d5SGuenter Roeck /*
328f4d325d5SGuenter Roeck  * Chip attributes are not attribute templates but actual sysfs attributes.
329f4d325d5SGuenter Roeck  * See hwmon_genattr() for special handling.
330f4d325d5SGuenter Roeck  */
331f4d325d5SGuenter Roeck static const char * const hwmon_chip_attrs[] = {
332d560168bSGuenter Roeck 	[hwmon_chip_temp_reset_history] = "temp_reset_history",
33300d616cfSGuenter Roeck 	[hwmon_chip_in_reset_history] = "in_reset_history",
3349b26947cSGuenter Roeck 	[hwmon_chip_curr_reset_history] = "curr_reset_history",
335b308f5c7SGuenter Roeck 	[hwmon_chip_power_reset_history] = "power_reset_history",
336d560168bSGuenter Roeck 	[hwmon_chip_update_interval] = "update_interval",
337d560168bSGuenter Roeck 	[hwmon_chip_alarms] = "alarms",
3389f00995eSGuenter Roeck 	[hwmon_chip_samples] = "samples",
3399f00995eSGuenter Roeck 	[hwmon_chip_curr_samples] = "curr_samples",
3409f00995eSGuenter Roeck 	[hwmon_chip_in_samples] = "in_samples",
3419f00995eSGuenter Roeck 	[hwmon_chip_power_samples] = "power_samples",
3429f00995eSGuenter Roeck 	[hwmon_chip_temp_samples] = "temp_samples",
343d560168bSGuenter Roeck };
344d560168bSGuenter Roeck 
345d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = {
346*002c6b54SGuenter Roeck 	[hwmon_temp_enable] = "temp%d_enable",
347d560168bSGuenter Roeck 	[hwmon_temp_input] = "temp%d_input",
348d560168bSGuenter Roeck 	[hwmon_temp_type] = "temp%d_type",
349d560168bSGuenter Roeck 	[hwmon_temp_lcrit] = "temp%d_lcrit",
350d560168bSGuenter Roeck 	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
351d560168bSGuenter Roeck 	[hwmon_temp_min] = "temp%d_min",
352d560168bSGuenter Roeck 	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
353d560168bSGuenter Roeck 	[hwmon_temp_max] = "temp%d_max",
354d560168bSGuenter Roeck 	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
355d560168bSGuenter Roeck 	[hwmon_temp_crit] = "temp%d_crit",
356d560168bSGuenter Roeck 	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
357d560168bSGuenter Roeck 	[hwmon_temp_emergency] = "temp%d_emergency",
358d560168bSGuenter Roeck 	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
359d560168bSGuenter Roeck 	[hwmon_temp_alarm] = "temp%d_alarm",
360d560168bSGuenter Roeck 	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
361d560168bSGuenter Roeck 	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
362d560168bSGuenter Roeck 	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
363d560168bSGuenter Roeck 	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
364d560168bSGuenter Roeck 	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
365d560168bSGuenter Roeck 	[hwmon_temp_fault] = "temp%d_fault",
366d560168bSGuenter Roeck 	[hwmon_temp_offset] = "temp%d_offset",
367d560168bSGuenter Roeck 	[hwmon_temp_label] = "temp%d_label",
368d560168bSGuenter Roeck 	[hwmon_temp_lowest] = "temp%d_lowest",
369d560168bSGuenter Roeck 	[hwmon_temp_highest] = "temp%d_highest",
370d560168bSGuenter Roeck 	[hwmon_temp_reset_history] = "temp%d_reset_history",
371d560168bSGuenter Roeck };
372d560168bSGuenter Roeck 
37300d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = {
374*002c6b54SGuenter Roeck 	[hwmon_in_enable] = "in%d_enable",
37500d616cfSGuenter Roeck 	[hwmon_in_input] = "in%d_input",
37600d616cfSGuenter Roeck 	[hwmon_in_min] = "in%d_min",
37700d616cfSGuenter Roeck 	[hwmon_in_max] = "in%d_max",
37800d616cfSGuenter Roeck 	[hwmon_in_lcrit] = "in%d_lcrit",
37900d616cfSGuenter Roeck 	[hwmon_in_crit] = "in%d_crit",
38000d616cfSGuenter Roeck 	[hwmon_in_average] = "in%d_average",
38100d616cfSGuenter Roeck 	[hwmon_in_lowest] = "in%d_lowest",
38200d616cfSGuenter Roeck 	[hwmon_in_highest] = "in%d_highest",
38300d616cfSGuenter Roeck 	[hwmon_in_reset_history] = "in%d_reset_history",
38400d616cfSGuenter Roeck 	[hwmon_in_label] = "in%d_label",
38500d616cfSGuenter Roeck 	[hwmon_in_alarm] = "in%d_alarm",
38600d616cfSGuenter Roeck 	[hwmon_in_min_alarm] = "in%d_min_alarm",
38700d616cfSGuenter Roeck 	[hwmon_in_max_alarm] = "in%d_max_alarm",
38800d616cfSGuenter Roeck 	[hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
38900d616cfSGuenter Roeck 	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
39000d616cfSGuenter Roeck };
39100d616cfSGuenter Roeck 
3929b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = {
393*002c6b54SGuenter Roeck 	[hwmon_curr_enable] = "curr%d_enable",
3949b26947cSGuenter Roeck 	[hwmon_curr_input] = "curr%d_input",
3959b26947cSGuenter Roeck 	[hwmon_curr_min] = "curr%d_min",
3969b26947cSGuenter Roeck 	[hwmon_curr_max] = "curr%d_max",
3979b26947cSGuenter Roeck 	[hwmon_curr_lcrit] = "curr%d_lcrit",
3989b26947cSGuenter Roeck 	[hwmon_curr_crit] = "curr%d_crit",
3999b26947cSGuenter Roeck 	[hwmon_curr_average] = "curr%d_average",
4009b26947cSGuenter Roeck 	[hwmon_curr_lowest] = "curr%d_lowest",
4019b26947cSGuenter Roeck 	[hwmon_curr_highest] = "curr%d_highest",
4029b26947cSGuenter Roeck 	[hwmon_curr_reset_history] = "curr%d_reset_history",
4039b26947cSGuenter Roeck 	[hwmon_curr_label] = "curr%d_label",
4049b26947cSGuenter Roeck 	[hwmon_curr_alarm] = "curr%d_alarm",
4059b26947cSGuenter Roeck 	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
4069b26947cSGuenter Roeck 	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
4079b26947cSGuenter Roeck 	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
4089b26947cSGuenter Roeck 	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
4099b26947cSGuenter Roeck };
4109b26947cSGuenter Roeck 
411b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = {
412*002c6b54SGuenter Roeck 	[hwmon_power_enable] = "power%d_enable",
413b308f5c7SGuenter Roeck 	[hwmon_power_average] = "power%d_average",
414b308f5c7SGuenter Roeck 	[hwmon_power_average_interval] = "power%d_average_interval",
415b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_max] = "power%d_interval_max",
416b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_min] = "power%d_interval_min",
417b308f5c7SGuenter Roeck 	[hwmon_power_average_highest] = "power%d_average_highest",
418b308f5c7SGuenter Roeck 	[hwmon_power_average_lowest] = "power%d_average_lowest",
419b308f5c7SGuenter Roeck 	[hwmon_power_average_max] = "power%d_average_max",
420b308f5c7SGuenter Roeck 	[hwmon_power_average_min] = "power%d_average_min",
421b308f5c7SGuenter Roeck 	[hwmon_power_input] = "power%d_input",
422b308f5c7SGuenter Roeck 	[hwmon_power_input_highest] = "power%d_input_highest",
423b308f5c7SGuenter Roeck 	[hwmon_power_input_lowest] = "power%d_input_lowest",
424b308f5c7SGuenter Roeck 	[hwmon_power_reset_history] = "power%d_reset_history",
425b308f5c7SGuenter Roeck 	[hwmon_power_accuracy] = "power%d_accuracy",
426b308f5c7SGuenter Roeck 	[hwmon_power_cap] = "power%d_cap",
427b308f5c7SGuenter Roeck 	[hwmon_power_cap_hyst] = "power%d_cap_hyst",
428b308f5c7SGuenter Roeck 	[hwmon_power_cap_max] = "power%d_cap_max",
429b308f5c7SGuenter Roeck 	[hwmon_power_cap_min] = "power%d_cap_min",
430aa7f29b0SAndrew Lunn 	[hwmon_power_min] = "power%d_min",
431b308f5c7SGuenter Roeck 	[hwmon_power_max] = "power%d_max",
432aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit] = "power%d_lcrit",
433b308f5c7SGuenter Roeck 	[hwmon_power_crit] = "power%d_crit",
434b308f5c7SGuenter Roeck 	[hwmon_power_label] = "power%d_label",
435b308f5c7SGuenter Roeck 	[hwmon_power_alarm] = "power%d_alarm",
436b308f5c7SGuenter Roeck 	[hwmon_power_cap_alarm] = "power%d_cap_alarm",
437aa7f29b0SAndrew Lunn 	[hwmon_power_min_alarm] = "power%d_min_alarm",
438b308f5c7SGuenter Roeck 	[hwmon_power_max_alarm] = "power%d_max_alarm",
439aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
440b308f5c7SGuenter Roeck 	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
441b308f5c7SGuenter Roeck };
442b308f5c7SGuenter Roeck 
4436bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = {
444*002c6b54SGuenter Roeck 	[hwmon_energy_enable] = "energy%d_enable",
4456bfcca44SGuenter Roeck 	[hwmon_energy_input] = "energy%d_input",
4466bfcca44SGuenter Roeck 	[hwmon_energy_label] = "energy%d_label",
4476bfcca44SGuenter Roeck };
4486bfcca44SGuenter Roeck 
4496bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = {
450*002c6b54SGuenter Roeck 	[hwmon_humidity_enable] = "humidity%d_enable",
4516bfcca44SGuenter Roeck 	[hwmon_humidity_input] = "humidity%d_input",
4526bfcca44SGuenter Roeck 	[hwmon_humidity_label] = "humidity%d_label",
4536bfcca44SGuenter Roeck 	[hwmon_humidity_min] = "humidity%d_min",
4546bfcca44SGuenter Roeck 	[hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
4556bfcca44SGuenter Roeck 	[hwmon_humidity_max] = "humidity%d_max",
4566bfcca44SGuenter Roeck 	[hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
4576bfcca44SGuenter Roeck 	[hwmon_humidity_alarm] = "humidity%d_alarm",
4586bfcca44SGuenter Roeck 	[hwmon_humidity_fault] = "humidity%d_fault",
4596bfcca44SGuenter Roeck };
4606bfcca44SGuenter Roeck 
4618faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = {
462*002c6b54SGuenter Roeck 	[hwmon_fan_enable] = "fan%d_enable",
4638faee73fSGuenter Roeck 	[hwmon_fan_input] = "fan%d_input",
4648faee73fSGuenter Roeck 	[hwmon_fan_label] = "fan%d_label",
4658faee73fSGuenter Roeck 	[hwmon_fan_min] = "fan%d_min",
4668faee73fSGuenter Roeck 	[hwmon_fan_max] = "fan%d_max",
4678faee73fSGuenter Roeck 	[hwmon_fan_div] = "fan%d_div",
4688faee73fSGuenter Roeck 	[hwmon_fan_pulses] = "fan%d_pulses",
4698faee73fSGuenter Roeck 	[hwmon_fan_target] = "fan%d_target",
4708faee73fSGuenter Roeck 	[hwmon_fan_alarm] = "fan%d_alarm",
4718faee73fSGuenter Roeck 	[hwmon_fan_min_alarm] = "fan%d_min_alarm",
4728faee73fSGuenter Roeck 	[hwmon_fan_max_alarm] = "fan%d_max_alarm",
4738faee73fSGuenter Roeck 	[hwmon_fan_fault] = "fan%d_fault",
4748faee73fSGuenter Roeck };
4758faee73fSGuenter Roeck 
476f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = {
477f9f7bb3aSGuenter Roeck 	[hwmon_pwm_input] = "pwm%d",
478f9f7bb3aSGuenter Roeck 	[hwmon_pwm_enable] = "pwm%d_enable",
479f9f7bb3aSGuenter Roeck 	[hwmon_pwm_mode] = "pwm%d_mode",
480f9f7bb3aSGuenter Roeck 	[hwmon_pwm_freq] = "pwm%d_freq",
481f9f7bb3aSGuenter Roeck };
482f9f7bb3aSGuenter Roeck 
4834413405fSDr. David Alan Gilbert static const char * const hwmon_intrusion_attr_templates[] = {
4844413405fSDr. David Alan Gilbert 	[hwmon_intrusion_alarm] = "intrusion%d_alarm",
4854413405fSDr. David Alan Gilbert 	[hwmon_intrusion_beep]  = "intrusion%d_beep",
4864413405fSDr. David Alan Gilbert };
4874413405fSDr. David Alan Gilbert 
488d560168bSGuenter Roeck static const char * const *__templates[] = {
489f4d325d5SGuenter Roeck 	[hwmon_chip] = hwmon_chip_attrs,
490d560168bSGuenter Roeck 	[hwmon_temp] = hwmon_temp_attr_templates,
49100d616cfSGuenter Roeck 	[hwmon_in] = hwmon_in_attr_templates,
4929b26947cSGuenter Roeck 	[hwmon_curr] = hwmon_curr_attr_templates,
493b308f5c7SGuenter Roeck 	[hwmon_power] = hwmon_power_attr_templates,
4946bfcca44SGuenter Roeck 	[hwmon_energy] = hwmon_energy_attr_templates,
4956bfcca44SGuenter Roeck 	[hwmon_humidity] = hwmon_humidity_attr_templates,
4968faee73fSGuenter Roeck 	[hwmon_fan] = hwmon_fan_attr_templates,
497f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = hwmon_pwm_attr_templates,
4984413405fSDr. David Alan Gilbert 	[hwmon_intrusion] = hwmon_intrusion_attr_templates,
499d560168bSGuenter Roeck };
500d560168bSGuenter Roeck 
501d560168bSGuenter Roeck static const int __templates_size[] = {
502f4d325d5SGuenter Roeck 	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
503d560168bSGuenter Roeck 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
50400d616cfSGuenter Roeck 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
5059b26947cSGuenter Roeck 	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
506b308f5c7SGuenter Roeck 	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
5076bfcca44SGuenter Roeck 	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
5086bfcca44SGuenter Roeck 	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
5098faee73fSGuenter Roeck 	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
510f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
5114413405fSDr. David Alan Gilbert 	[hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
512d560168bSGuenter Roeck };
513d560168bSGuenter Roeck 
514d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
515d560168bSGuenter Roeck {
516d560168bSGuenter Roeck 	int i, n;
517d560168bSGuenter Roeck 
518d560168bSGuenter Roeck 	for (i = n = 0; info->config[i]; i++)
519d560168bSGuenter Roeck 		n += hweight32(info->config[i]);
520d560168bSGuenter Roeck 
521d560168bSGuenter Roeck 	return n;
522d560168bSGuenter Roeck }
523d560168bSGuenter Roeck 
5243bf8bdcfSGuenter Roeck static int hwmon_genattrs(const void *drvdata,
525d560168bSGuenter Roeck 			  struct attribute **attrs,
526d560168bSGuenter Roeck 			  const struct hwmon_ops *ops,
527d560168bSGuenter Roeck 			  const struct hwmon_channel_info *info)
528d560168bSGuenter Roeck {
529d560168bSGuenter Roeck 	const char * const *templates;
530d560168bSGuenter Roeck 	int template_size;
531d560168bSGuenter Roeck 	int i, aindex = 0;
532d560168bSGuenter Roeck 
533d560168bSGuenter Roeck 	if (info->type >= ARRAY_SIZE(__templates))
534d560168bSGuenter Roeck 		return -EINVAL;
535d560168bSGuenter Roeck 
536d560168bSGuenter Roeck 	templates = __templates[info->type];
537d560168bSGuenter Roeck 	template_size = __templates_size[info->type];
538d560168bSGuenter Roeck 
539d560168bSGuenter Roeck 	for (i = 0; info->config[i]; i++) {
540d560168bSGuenter Roeck 		u32 attr_mask = info->config[i];
541d560168bSGuenter Roeck 		u32 attr;
542d560168bSGuenter Roeck 
543d560168bSGuenter Roeck 		while (attr_mask) {
544d560168bSGuenter Roeck 			struct attribute *a;
545d560168bSGuenter Roeck 
546d560168bSGuenter Roeck 			attr = __ffs(attr_mask);
547d560168bSGuenter Roeck 			attr_mask &= ~BIT(attr);
548d560168bSGuenter Roeck 			if (attr >= template_size)
549d560168bSGuenter Roeck 				return -EINVAL;
5503bf8bdcfSGuenter Roeck 			a = hwmon_genattr(drvdata, info->type, attr, i,
551d560168bSGuenter Roeck 					  templates[attr], ops);
552d560168bSGuenter Roeck 			if (IS_ERR(a)) {
553d560168bSGuenter Roeck 				if (PTR_ERR(a) != -ENOENT)
554d560168bSGuenter Roeck 					return PTR_ERR(a);
555d560168bSGuenter Roeck 				continue;
556d560168bSGuenter Roeck 			}
557d560168bSGuenter Roeck 			attrs[aindex++] = a;
558d560168bSGuenter Roeck 		}
559d560168bSGuenter Roeck 	}
560d560168bSGuenter Roeck 	return aindex;
561d560168bSGuenter Roeck }
562d560168bSGuenter Roeck 
563d560168bSGuenter Roeck static struct attribute **
5643bf8bdcfSGuenter Roeck __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
565d560168bSGuenter Roeck {
566d560168bSGuenter Roeck 	int ret, i, aindex = 0, nattrs = 0;
567d560168bSGuenter Roeck 	struct attribute **attrs;
568d560168bSGuenter Roeck 
569d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++)
570d560168bSGuenter Roeck 		nattrs += hwmon_num_channel_attrs(chip->info[i]);
571d560168bSGuenter Roeck 
572d560168bSGuenter Roeck 	if (nattrs == 0)
573d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
574d560168bSGuenter Roeck 
5753bf8bdcfSGuenter Roeck 	attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
576d560168bSGuenter Roeck 	if (!attrs)
577d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
578d560168bSGuenter Roeck 
579d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++) {
5803bf8bdcfSGuenter Roeck 		ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
581d560168bSGuenter Roeck 				     chip->info[i]);
5823bf8bdcfSGuenter Roeck 		if (ret < 0) {
5833bf8bdcfSGuenter Roeck 			hwmon_free_attrs(attrs);
584d560168bSGuenter Roeck 			return ERR_PTR(ret);
5853bf8bdcfSGuenter Roeck 		}
586d560168bSGuenter Roeck 		aindex += ret;
587d560168bSGuenter Roeck 	}
588d560168bSGuenter Roeck 
589d560168bSGuenter Roeck 	return attrs;
590d560168bSGuenter Roeck }
591d560168bSGuenter Roeck 
592d560168bSGuenter Roeck static struct device *
593d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
594d560168bSGuenter Roeck 			const struct hwmon_chip_info *chip,
595d560168bSGuenter Roeck 			const struct attribute_group **groups)
596d560168bSGuenter Roeck {
597d560168bSGuenter Roeck 	struct hwmon_device *hwdev;
598d560168bSGuenter Roeck 	struct device *hdev;
599d560168bSGuenter Roeck 	int i, j, err, id;
600d560168bSGuenter Roeck 
60174d3b641SGuenter Roeck 	/* Complain about invalid characters in hwmon name attribute */
602d560168bSGuenter Roeck 	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
60374d3b641SGuenter Roeck 		dev_warn(dev,
60474d3b641SGuenter Roeck 			 "hwmon: '%s' is not a valid name attribute, please fix\n",
60574d3b641SGuenter Roeck 			 name);
606d560168bSGuenter Roeck 
607d560168bSGuenter Roeck 	id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
608d560168bSGuenter Roeck 	if (id < 0)
609d560168bSGuenter Roeck 		return ERR_PTR(id);
610d560168bSGuenter Roeck 
611d560168bSGuenter Roeck 	hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
612d560168bSGuenter Roeck 	if (hwdev == NULL) {
613d560168bSGuenter Roeck 		err = -ENOMEM;
614d560168bSGuenter Roeck 		goto ida_remove;
615d560168bSGuenter Roeck 	}
616d560168bSGuenter Roeck 
617d560168bSGuenter Roeck 	hdev = &hwdev->dev;
618d560168bSGuenter Roeck 
619239552f4SGuenter Roeck 	if (chip) {
620d560168bSGuenter Roeck 		struct attribute **attrs;
621b2a4cc3aSGuenter Roeck 		int ngroups = 2; /* terminating NULL plus &hwdev->groups */
622d560168bSGuenter Roeck 
623d560168bSGuenter Roeck 		if (groups)
624d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
625d560168bSGuenter Roeck 				ngroups++;
626d560168bSGuenter Roeck 
6273bf8bdcfSGuenter Roeck 		hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
62838d8ed65SColin Ian King 		if (!hwdev->groups) {
62938d8ed65SColin Ian King 			err = -ENOMEM;
63038d8ed65SColin Ian King 			goto free_hwmon;
63138d8ed65SColin Ian King 		}
632d560168bSGuenter Roeck 
6333bf8bdcfSGuenter Roeck 		attrs = __hwmon_create_attrs(drvdata, chip);
634d560168bSGuenter Roeck 		if (IS_ERR(attrs)) {
635d560168bSGuenter Roeck 			err = PTR_ERR(attrs);
636d560168bSGuenter Roeck 			goto free_hwmon;
637d560168bSGuenter Roeck 		}
638d560168bSGuenter Roeck 
639d560168bSGuenter Roeck 		hwdev->group.attrs = attrs;
640d560168bSGuenter Roeck 		ngroups = 0;
641d560168bSGuenter Roeck 		hwdev->groups[ngroups++] = &hwdev->group;
642d560168bSGuenter Roeck 
643d560168bSGuenter Roeck 		if (groups) {
644d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
645d560168bSGuenter Roeck 				hwdev->groups[ngroups++] = groups[i];
646d560168bSGuenter Roeck 		}
647d560168bSGuenter Roeck 
648d560168bSGuenter Roeck 		hdev->groups = hwdev->groups;
649d560168bSGuenter Roeck 	} else {
650d560168bSGuenter Roeck 		hdev->groups = groups;
651d560168bSGuenter Roeck 	}
652d560168bSGuenter Roeck 
653d560168bSGuenter Roeck 	hwdev->name = name;
654d560168bSGuenter Roeck 	hdev->class = &hwmon_class;
655d560168bSGuenter Roeck 	hdev->parent = dev;
656d560168bSGuenter Roeck 	hdev->of_node = dev ? dev->of_node : NULL;
657d560168bSGuenter Roeck 	hwdev->chip = chip;
658d560168bSGuenter Roeck 	dev_set_drvdata(hdev, drvdata);
659d560168bSGuenter Roeck 	dev_set_name(hdev, HWMON_ID_FORMAT, id);
660d560168bSGuenter Roeck 	err = device_register(hdev);
661d560168bSGuenter Roeck 	if (err)
662d560168bSGuenter Roeck 		goto free_hwmon;
663d560168bSGuenter Roeck 
664c41dd48eSEduardo Valentin 	if (dev && dev->of_node && chip && chip->ops->read &&
665d560168bSGuenter Roeck 	    chip->info[0]->type == hwmon_chip &&
666d560168bSGuenter Roeck 	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
667d560168bSGuenter Roeck 		const struct hwmon_channel_info **info = chip->info;
668d560168bSGuenter Roeck 
669d560168bSGuenter Roeck 		for (i = 1; info[i]; i++) {
670d560168bSGuenter Roeck 			if (info[i]->type != hwmon_temp)
671d560168bSGuenter Roeck 				continue;
672d560168bSGuenter Roeck 
673d560168bSGuenter Roeck 			for (j = 0; info[i]->config[j]; j++) {
674d560168bSGuenter Roeck 				if (!chip->ops->is_visible(drvdata, hwmon_temp,
675d560168bSGuenter Roeck 							   hwmon_temp_input, j))
676d560168bSGuenter Roeck 					continue;
67747c332deSLinus Walleij 				if (info[i]->config[j] & HWMON_T_INPUT) {
6783bf8bdcfSGuenter Roeck 					err = hwmon_thermal_add_sensor(hdev, j);
67974e35127SDmitry Osipenko 					if (err) {
68074e35127SDmitry Osipenko 						device_unregister(hdev);
681792eac18SGuenter Roeck 						/*
682792eac18SGuenter Roeck 						 * Don't worry about hwdev;
683792eac18SGuenter Roeck 						 * hwmon_dev_release(), called
684792eac18SGuenter Roeck 						 * from device_unregister(),
685792eac18SGuenter Roeck 						 * will free it.
686792eac18SGuenter Roeck 						 */
68774e35127SDmitry Osipenko 						goto ida_remove;
68874e35127SDmitry Osipenko 					}
68947c332deSLinus Walleij 				}
690d560168bSGuenter Roeck 			}
691d560168bSGuenter Roeck 		}
692d560168bSGuenter Roeck 	}
693d560168bSGuenter Roeck 
694d560168bSGuenter Roeck 	return hdev;
695d560168bSGuenter Roeck 
696d560168bSGuenter Roeck free_hwmon:
6973bf8bdcfSGuenter Roeck 	hwmon_dev_release(hdev);
698d560168bSGuenter Roeck ida_remove:
699d560168bSGuenter Roeck 	ida_simple_remove(&hwmon_ida, id);
700d560168bSGuenter Roeck 	return ERR_PTR(err);
701d560168bSGuenter Roeck }
702d560168bSGuenter Roeck 
7031236441fSMark M. Hoffman /**
704bab2243cSGuenter Roeck  * hwmon_device_register_with_groups - register w/ hwmon
705bab2243cSGuenter Roeck  * @dev: the parent device
706bab2243cSGuenter Roeck  * @name: hwmon name attribute
707bab2243cSGuenter Roeck  * @drvdata: driver data to attach to created device
708bab2243cSGuenter Roeck  * @groups: List of attribute groups to create
709bab2243cSGuenter Roeck  *
710bab2243cSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
711bab2243cSGuenter Roeck  * longer needed.
712bab2243cSGuenter Roeck  *
713bab2243cSGuenter Roeck  * Returns the pointer to the new device.
714bab2243cSGuenter Roeck  */
715bab2243cSGuenter Roeck struct device *
716bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name,
717bab2243cSGuenter Roeck 				  void *drvdata,
718bab2243cSGuenter Roeck 				  const struct attribute_group **groups)
719bab2243cSGuenter Roeck {
7208353863aSGuenter Roeck 	if (!name)
7218353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
7228353863aSGuenter Roeck 
723d560168bSGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
724bab2243cSGuenter Roeck }
725bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
726bab2243cSGuenter Roeck 
727bab2243cSGuenter Roeck /**
728d560168bSGuenter Roeck  * hwmon_device_register_with_info - register w/ hwmon
729d560168bSGuenter Roeck  * @dev: the parent device
730d560168bSGuenter Roeck  * @name: hwmon name attribute
731d560168bSGuenter Roeck  * @drvdata: driver data to attach to created device
7323870945aSGuenter Roeck  * @chip: pointer to hwmon chip information
733848ba0a2SGuenter Roeck  * @extra_groups: pointer to list of additional non-standard attribute groups
734d560168bSGuenter Roeck  *
735d560168bSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
736d560168bSGuenter Roeck  * longer needed.
737d560168bSGuenter Roeck  *
738d560168bSGuenter Roeck  * Returns the pointer to the new device.
739d560168bSGuenter Roeck  */
740d560168bSGuenter Roeck struct device *
741d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name,
742d560168bSGuenter Roeck 				void *drvdata,
743d560168bSGuenter Roeck 				const struct hwmon_chip_info *chip,
744848ba0a2SGuenter Roeck 				const struct attribute_group **extra_groups)
745d560168bSGuenter Roeck {
7468353863aSGuenter Roeck 	if (!name)
7478353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
7488353863aSGuenter Roeck 
749239552f4SGuenter Roeck 	if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
750d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
751d560168bSGuenter Roeck 
75259df4f4eSLucas Magasweran 	if (chip && !dev)
75359df4f4eSLucas Magasweran 		return ERR_PTR(-EINVAL);
75459df4f4eSLucas Magasweran 
755848ba0a2SGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
756d560168bSGuenter Roeck }
757d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
758d560168bSGuenter Roeck 
759d560168bSGuenter Roeck /**
7601beeffe4STony Jones  * hwmon_device_register - register w/ hwmon
7611236441fSMark M. Hoffman  * @dev: the device to register
7621236441fSMark M. Hoffman  *
7631beeffe4STony Jones  * hwmon_device_unregister() must be called when the device is no
7641236441fSMark M. Hoffman  * longer needed.
7651236441fSMark M. Hoffman  *
7661beeffe4STony Jones  * Returns the pointer to the new device.
7671236441fSMark M. Hoffman  */
7681beeffe4STony Jones struct device *hwmon_device_register(struct device *dev)
7691236441fSMark M. Hoffman {
770af1bd36cSGuenter Roeck 	dev_warn(dev,
771af1bd36cSGuenter Roeck 		 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
772af1bd36cSGuenter Roeck 
7738353863aSGuenter Roeck 	return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
7741236441fSMark M. Hoffman }
775839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register);
7761236441fSMark M. Hoffman 
7771236441fSMark M. Hoffman /**
7781236441fSMark M. Hoffman  * hwmon_device_unregister - removes the previously registered class device
7791236441fSMark M. Hoffman  *
7801beeffe4STony Jones  * @dev: the class device to destroy
7811236441fSMark M. Hoffman  */
7821beeffe4STony Jones void hwmon_device_unregister(struct device *dev)
7831236441fSMark M. Hoffman {
7841236441fSMark M. Hoffman 	int id;
7851236441fSMark M. Hoffman 
786739cf3a2SKay Sievers 	if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
7871beeffe4STony Jones 		device_unregister(dev);
7884ca5f468SJonathan Cameron 		ida_simple_remove(&hwmon_ida, id);
7891236441fSMark M. Hoffman 	} else
7901beeffe4STony Jones 		dev_dbg(dev->parent,
7911236441fSMark M. Hoffman 			"hwmon_device_unregister() failed: bad class ID!\n");
7921236441fSMark M. Hoffman }
793839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister);
7941236441fSMark M. Hoffman 
79574188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res)
79674188cbaSGuenter Roeck {
79774188cbaSGuenter Roeck 	struct device *hwdev = *(struct device **)res;
79874188cbaSGuenter Roeck 
79974188cbaSGuenter Roeck 	hwmon_device_unregister(hwdev);
80074188cbaSGuenter Roeck }
80174188cbaSGuenter Roeck 
80274188cbaSGuenter Roeck /**
80374188cbaSGuenter Roeck  * devm_hwmon_device_register_with_groups - register w/ hwmon
80474188cbaSGuenter Roeck  * @dev: the parent device
80574188cbaSGuenter Roeck  * @name: hwmon name attribute
80674188cbaSGuenter Roeck  * @drvdata: driver data to attach to created device
80774188cbaSGuenter Roeck  * @groups: List of attribute groups to create
80874188cbaSGuenter Roeck  *
80974188cbaSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
81074188cbaSGuenter Roeck  * unregistered with the parent device.
81174188cbaSGuenter Roeck  */
81274188cbaSGuenter Roeck struct device *
81374188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
81474188cbaSGuenter Roeck 				       void *drvdata,
81574188cbaSGuenter Roeck 				       const struct attribute_group **groups)
81674188cbaSGuenter Roeck {
81774188cbaSGuenter Roeck 	struct device **ptr, *hwdev;
81874188cbaSGuenter Roeck 
81974188cbaSGuenter Roeck 	if (!dev)
82074188cbaSGuenter Roeck 		return ERR_PTR(-EINVAL);
82174188cbaSGuenter Roeck 
82274188cbaSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
82374188cbaSGuenter Roeck 	if (!ptr)
82474188cbaSGuenter Roeck 		return ERR_PTR(-ENOMEM);
82574188cbaSGuenter Roeck 
82674188cbaSGuenter Roeck 	hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
82774188cbaSGuenter Roeck 	if (IS_ERR(hwdev))
82874188cbaSGuenter Roeck 		goto error;
82974188cbaSGuenter Roeck 
83074188cbaSGuenter Roeck 	*ptr = hwdev;
83174188cbaSGuenter Roeck 	devres_add(dev, ptr);
83274188cbaSGuenter Roeck 	return hwdev;
83374188cbaSGuenter Roeck 
83474188cbaSGuenter Roeck error:
83574188cbaSGuenter Roeck 	devres_free(ptr);
83674188cbaSGuenter Roeck 	return hwdev;
83774188cbaSGuenter Roeck }
83874188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
83974188cbaSGuenter Roeck 
840d560168bSGuenter Roeck /**
841d560168bSGuenter Roeck  * devm_hwmon_device_register_with_info - register w/ hwmon
842d560168bSGuenter Roeck  * @dev:	the parent device
843d560168bSGuenter Roeck  * @name:	hwmon name attribute
844d560168bSGuenter Roeck  * @drvdata:	driver data to attach to created device
8453870945aSGuenter Roeck  * @chip:	pointer to hwmon chip information
8463870945aSGuenter Roeck  * @groups:	pointer to list of driver specific attribute groups
847d560168bSGuenter Roeck  *
848d560168bSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
849d560168bSGuenter Roeck  * unregistered with the parent device.
850d560168bSGuenter Roeck  */
851d560168bSGuenter Roeck struct device *
852d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name,
853d560168bSGuenter Roeck 				     void *drvdata,
854d560168bSGuenter Roeck 				     const struct hwmon_chip_info *chip,
855d560168bSGuenter Roeck 				     const struct attribute_group **groups)
856d560168bSGuenter Roeck {
857d560168bSGuenter Roeck 	struct device **ptr, *hwdev;
858d560168bSGuenter Roeck 
859d560168bSGuenter Roeck 	if (!dev)
860d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
861d560168bSGuenter Roeck 
862d560168bSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
863d560168bSGuenter Roeck 	if (!ptr)
864d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
865d560168bSGuenter Roeck 
866d560168bSGuenter Roeck 	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
867d560168bSGuenter Roeck 						groups);
868d560168bSGuenter Roeck 	if (IS_ERR(hwdev))
869d560168bSGuenter Roeck 		goto error;
870d560168bSGuenter Roeck 
871d560168bSGuenter Roeck 	*ptr = hwdev;
872d560168bSGuenter Roeck 	devres_add(dev, ptr);
873d560168bSGuenter Roeck 
874d560168bSGuenter Roeck 	return hwdev;
875d560168bSGuenter Roeck 
876d560168bSGuenter Roeck error:
877d560168bSGuenter Roeck 	devres_free(ptr);
878d560168bSGuenter Roeck 	return hwdev;
879d560168bSGuenter Roeck }
880d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
881d560168bSGuenter Roeck 
88274188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data)
88374188cbaSGuenter Roeck {
88474188cbaSGuenter Roeck 	struct device **hwdev = res;
88574188cbaSGuenter Roeck 
88674188cbaSGuenter Roeck 	return *hwdev == data;
88774188cbaSGuenter Roeck }
88874188cbaSGuenter Roeck 
88974188cbaSGuenter Roeck /**
89074188cbaSGuenter Roeck  * devm_hwmon_device_unregister - removes a previously registered hwmon device
89174188cbaSGuenter Roeck  *
89274188cbaSGuenter Roeck  * @dev: the parent device of the device to unregister
89374188cbaSGuenter Roeck  */
89474188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev)
89574188cbaSGuenter Roeck {
89674188cbaSGuenter Roeck 	WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
89774188cbaSGuenter Roeck }
89874188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
89974188cbaSGuenter Roeck 
9002958b1ecSJean Delvare static void __init hwmon_pci_quirks(void)
9012958b1ecSJean Delvare {
9022958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI
9032958b1ecSJean Delvare 	struct pci_dev *sb;
9042958b1ecSJean Delvare 	u16 base;
9052958b1ecSJean Delvare 	u8 enable;
9062958b1ecSJean Delvare 
9072958b1ecSJean Delvare 	/* Open access to 0x295-0x296 on MSI MS-7031 */
9082958b1ecSJean Delvare 	sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
909d6dab7ddSJean Delvare 	if (sb) {
910d6dab7ddSJean Delvare 		if (sb->subsystem_vendor == 0x1462 &&	/* MSI */
911d6dab7ddSJean Delvare 		    sb->subsystem_device == 0x0031) {	/* MS-7031 */
9122958b1ecSJean Delvare 			pci_read_config_byte(sb, 0x48, &enable);
9132958b1ecSJean Delvare 			pci_read_config_word(sb, 0x64, &base);
9142958b1ecSJean Delvare 
9152958b1ecSJean Delvare 			if (base == 0 && !(enable & BIT(2))) {
9162958b1ecSJean Delvare 				dev_info(&sb->dev,
9172958b1ecSJean Delvare 					 "Opening wide generic port at 0x295\n");
9182958b1ecSJean Delvare 				pci_write_config_word(sb, 0x64, 0x295);
919d6dab7ddSJean Delvare 				pci_write_config_byte(sb, 0x48,
920d6dab7ddSJean Delvare 						      enable | BIT(2));
9212958b1ecSJean Delvare 			}
9222958b1ecSJean Delvare 		}
923d6dab7ddSJean Delvare 		pci_dev_put(sb);
924d6dab7ddSJean Delvare 	}
9252958b1ecSJean Delvare #endif
9262958b1ecSJean Delvare }
9272958b1ecSJean Delvare 
9281236441fSMark M. Hoffman static int __init hwmon_init(void)
9291236441fSMark M. Hoffman {
930bab2243cSGuenter Roeck 	int err;
931bab2243cSGuenter Roeck 
9322958b1ecSJean Delvare 	hwmon_pci_quirks();
9332958b1ecSJean Delvare 
934bab2243cSGuenter Roeck 	err = class_register(&hwmon_class);
935bab2243cSGuenter Roeck 	if (err) {
936bab2243cSGuenter Roeck 		pr_err("couldn't register hwmon sysfs class\n");
937bab2243cSGuenter Roeck 		return err;
9381236441fSMark M. Hoffman 	}
9391236441fSMark M. Hoffman 	return 0;
9401236441fSMark M. Hoffman }
9411236441fSMark M. Hoffman 
9421236441fSMark M. Hoffman static void __exit hwmon_exit(void)
9431236441fSMark M. Hoffman {
944bab2243cSGuenter Roeck 	class_unregister(&hwmon_class);
9451236441fSMark M. Hoffman }
9461236441fSMark M. Hoffman 
94737f54ee5SDavid Brownell subsys_initcall(hwmon_init);
9481236441fSMark M. Hoffman module_exit(hwmon_exit);
9491236441fSMark M. Hoffman 
9501236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
9511236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
9521236441fSMark M. Hoffman MODULE_LICENSE("GPL");
9531236441fSMark M. Hoffman 
954