xref: /openbmc/linux/drivers/hwmon/hwmon.c (revision 4413405f931ef97ab1263ae3588e6f656ec220b7)
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 {
191*4413405fSDr. 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[] = {
346d560168bSGuenter Roeck 	[hwmon_temp_input] = "temp%d_input",
347d560168bSGuenter Roeck 	[hwmon_temp_type] = "temp%d_type",
348d560168bSGuenter Roeck 	[hwmon_temp_lcrit] = "temp%d_lcrit",
349d560168bSGuenter Roeck 	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
350d560168bSGuenter Roeck 	[hwmon_temp_min] = "temp%d_min",
351d560168bSGuenter Roeck 	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
352d560168bSGuenter Roeck 	[hwmon_temp_max] = "temp%d_max",
353d560168bSGuenter Roeck 	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
354d560168bSGuenter Roeck 	[hwmon_temp_crit] = "temp%d_crit",
355d560168bSGuenter Roeck 	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
356d560168bSGuenter Roeck 	[hwmon_temp_emergency] = "temp%d_emergency",
357d560168bSGuenter Roeck 	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
358d560168bSGuenter Roeck 	[hwmon_temp_alarm] = "temp%d_alarm",
359d560168bSGuenter Roeck 	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
360d560168bSGuenter Roeck 	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
361d560168bSGuenter Roeck 	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
362d560168bSGuenter Roeck 	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
363d560168bSGuenter Roeck 	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
364d560168bSGuenter Roeck 	[hwmon_temp_fault] = "temp%d_fault",
365d560168bSGuenter Roeck 	[hwmon_temp_offset] = "temp%d_offset",
366d560168bSGuenter Roeck 	[hwmon_temp_label] = "temp%d_label",
367d560168bSGuenter Roeck 	[hwmon_temp_lowest] = "temp%d_lowest",
368d560168bSGuenter Roeck 	[hwmon_temp_highest] = "temp%d_highest",
369d560168bSGuenter Roeck 	[hwmon_temp_reset_history] = "temp%d_reset_history",
370d560168bSGuenter Roeck };
371d560168bSGuenter Roeck 
37200d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = {
37300d616cfSGuenter Roeck 	[hwmon_in_input] = "in%d_input",
37400d616cfSGuenter Roeck 	[hwmon_in_min] = "in%d_min",
37500d616cfSGuenter Roeck 	[hwmon_in_max] = "in%d_max",
37600d616cfSGuenter Roeck 	[hwmon_in_lcrit] = "in%d_lcrit",
37700d616cfSGuenter Roeck 	[hwmon_in_crit] = "in%d_crit",
37800d616cfSGuenter Roeck 	[hwmon_in_average] = "in%d_average",
37900d616cfSGuenter Roeck 	[hwmon_in_lowest] = "in%d_lowest",
38000d616cfSGuenter Roeck 	[hwmon_in_highest] = "in%d_highest",
38100d616cfSGuenter Roeck 	[hwmon_in_reset_history] = "in%d_reset_history",
38200d616cfSGuenter Roeck 	[hwmon_in_label] = "in%d_label",
38300d616cfSGuenter Roeck 	[hwmon_in_alarm] = "in%d_alarm",
38400d616cfSGuenter Roeck 	[hwmon_in_min_alarm] = "in%d_min_alarm",
38500d616cfSGuenter Roeck 	[hwmon_in_max_alarm] = "in%d_max_alarm",
38600d616cfSGuenter Roeck 	[hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
38700d616cfSGuenter Roeck 	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
38868c0d69dSNicolin Chen 	[hwmon_in_enable] = "in%d_enable",
38900d616cfSGuenter Roeck };
39000d616cfSGuenter Roeck 
3919b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = {
3929b26947cSGuenter Roeck 	[hwmon_curr_input] = "curr%d_input",
3939b26947cSGuenter Roeck 	[hwmon_curr_min] = "curr%d_min",
3949b26947cSGuenter Roeck 	[hwmon_curr_max] = "curr%d_max",
3959b26947cSGuenter Roeck 	[hwmon_curr_lcrit] = "curr%d_lcrit",
3969b26947cSGuenter Roeck 	[hwmon_curr_crit] = "curr%d_crit",
3979b26947cSGuenter Roeck 	[hwmon_curr_average] = "curr%d_average",
3989b26947cSGuenter Roeck 	[hwmon_curr_lowest] = "curr%d_lowest",
3999b26947cSGuenter Roeck 	[hwmon_curr_highest] = "curr%d_highest",
4009b26947cSGuenter Roeck 	[hwmon_curr_reset_history] = "curr%d_reset_history",
4019b26947cSGuenter Roeck 	[hwmon_curr_label] = "curr%d_label",
4029b26947cSGuenter Roeck 	[hwmon_curr_alarm] = "curr%d_alarm",
4039b26947cSGuenter Roeck 	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
4049b26947cSGuenter Roeck 	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
4059b26947cSGuenter Roeck 	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
4069b26947cSGuenter Roeck 	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
4079b26947cSGuenter Roeck };
4089b26947cSGuenter Roeck 
409b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = {
410b308f5c7SGuenter Roeck 	[hwmon_power_average] = "power%d_average",
411b308f5c7SGuenter Roeck 	[hwmon_power_average_interval] = "power%d_average_interval",
412b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_max] = "power%d_interval_max",
413b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_min] = "power%d_interval_min",
414b308f5c7SGuenter Roeck 	[hwmon_power_average_highest] = "power%d_average_highest",
415b308f5c7SGuenter Roeck 	[hwmon_power_average_lowest] = "power%d_average_lowest",
416b308f5c7SGuenter Roeck 	[hwmon_power_average_max] = "power%d_average_max",
417b308f5c7SGuenter Roeck 	[hwmon_power_average_min] = "power%d_average_min",
418b308f5c7SGuenter Roeck 	[hwmon_power_input] = "power%d_input",
419b308f5c7SGuenter Roeck 	[hwmon_power_input_highest] = "power%d_input_highest",
420b308f5c7SGuenter Roeck 	[hwmon_power_input_lowest] = "power%d_input_lowest",
421b308f5c7SGuenter Roeck 	[hwmon_power_reset_history] = "power%d_reset_history",
422b308f5c7SGuenter Roeck 	[hwmon_power_accuracy] = "power%d_accuracy",
423b308f5c7SGuenter Roeck 	[hwmon_power_cap] = "power%d_cap",
424b308f5c7SGuenter Roeck 	[hwmon_power_cap_hyst] = "power%d_cap_hyst",
425b308f5c7SGuenter Roeck 	[hwmon_power_cap_max] = "power%d_cap_max",
426b308f5c7SGuenter Roeck 	[hwmon_power_cap_min] = "power%d_cap_min",
427aa7f29b0SAndrew Lunn 	[hwmon_power_min] = "power%d_min",
428b308f5c7SGuenter Roeck 	[hwmon_power_max] = "power%d_max",
429aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit] = "power%d_lcrit",
430b308f5c7SGuenter Roeck 	[hwmon_power_crit] = "power%d_crit",
431b308f5c7SGuenter Roeck 	[hwmon_power_label] = "power%d_label",
432b308f5c7SGuenter Roeck 	[hwmon_power_alarm] = "power%d_alarm",
433b308f5c7SGuenter Roeck 	[hwmon_power_cap_alarm] = "power%d_cap_alarm",
434aa7f29b0SAndrew Lunn 	[hwmon_power_min_alarm] = "power%d_min_alarm",
435b308f5c7SGuenter Roeck 	[hwmon_power_max_alarm] = "power%d_max_alarm",
436aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
437b308f5c7SGuenter Roeck 	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
438b308f5c7SGuenter Roeck };
439b308f5c7SGuenter Roeck 
4406bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = {
4416bfcca44SGuenter Roeck 	[hwmon_energy_input] = "energy%d_input",
4426bfcca44SGuenter Roeck 	[hwmon_energy_label] = "energy%d_label",
4436bfcca44SGuenter Roeck };
4446bfcca44SGuenter Roeck 
4456bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = {
4466bfcca44SGuenter Roeck 	[hwmon_humidity_input] = "humidity%d_input",
4476bfcca44SGuenter Roeck 	[hwmon_humidity_label] = "humidity%d_label",
4486bfcca44SGuenter Roeck 	[hwmon_humidity_min] = "humidity%d_min",
4496bfcca44SGuenter Roeck 	[hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
4506bfcca44SGuenter Roeck 	[hwmon_humidity_max] = "humidity%d_max",
4516bfcca44SGuenter Roeck 	[hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
4526bfcca44SGuenter Roeck 	[hwmon_humidity_alarm] = "humidity%d_alarm",
4536bfcca44SGuenter Roeck 	[hwmon_humidity_fault] = "humidity%d_fault",
4546bfcca44SGuenter Roeck };
4556bfcca44SGuenter Roeck 
4568faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = {
4578faee73fSGuenter Roeck 	[hwmon_fan_input] = "fan%d_input",
4588faee73fSGuenter Roeck 	[hwmon_fan_label] = "fan%d_label",
4598faee73fSGuenter Roeck 	[hwmon_fan_min] = "fan%d_min",
4608faee73fSGuenter Roeck 	[hwmon_fan_max] = "fan%d_max",
4618faee73fSGuenter Roeck 	[hwmon_fan_div] = "fan%d_div",
4628faee73fSGuenter Roeck 	[hwmon_fan_pulses] = "fan%d_pulses",
4638faee73fSGuenter Roeck 	[hwmon_fan_target] = "fan%d_target",
4648faee73fSGuenter Roeck 	[hwmon_fan_alarm] = "fan%d_alarm",
4658faee73fSGuenter Roeck 	[hwmon_fan_min_alarm] = "fan%d_min_alarm",
4668faee73fSGuenter Roeck 	[hwmon_fan_max_alarm] = "fan%d_max_alarm",
4678faee73fSGuenter Roeck 	[hwmon_fan_fault] = "fan%d_fault",
4688faee73fSGuenter Roeck };
4698faee73fSGuenter Roeck 
470f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = {
471f9f7bb3aSGuenter Roeck 	[hwmon_pwm_input] = "pwm%d",
472f9f7bb3aSGuenter Roeck 	[hwmon_pwm_enable] = "pwm%d_enable",
473f9f7bb3aSGuenter Roeck 	[hwmon_pwm_mode] = "pwm%d_mode",
474f9f7bb3aSGuenter Roeck 	[hwmon_pwm_freq] = "pwm%d_freq",
475f9f7bb3aSGuenter Roeck };
476f9f7bb3aSGuenter Roeck 
477*4413405fSDr. David Alan Gilbert static const char * const hwmon_intrusion_attr_templates[] = {
478*4413405fSDr. David Alan Gilbert 	[hwmon_intrusion_alarm] = "intrusion%d_alarm",
479*4413405fSDr. David Alan Gilbert 	[hwmon_intrusion_beep]  = "intrusion%d_beep",
480*4413405fSDr. David Alan Gilbert };
481*4413405fSDr. David Alan Gilbert 
482d560168bSGuenter Roeck static const char * const *__templates[] = {
483f4d325d5SGuenter Roeck 	[hwmon_chip] = hwmon_chip_attrs,
484d560168bSGuenter Roeck 	[hwmon_temp] = hwmon_temp_attr_templates,
48500d616cfSGuenter Roeck 	[hwmon_in] = hwmon_in_attr_templates,
4869b26947cSGuenter Roeck 	[hwmon_curr] = hwmon_curr_attr_templates,
487b308f5c7SGuenter Roeck 	[hwmon_power] = hwmon_power_attr_templates,
4886bfcca44SGuenter Roeck 	[hwmon_energy] = hwmon_energy_attr_templates,
4896bfcca44SGuenter Roeck 	[hwmon_humidity] = hwmon_humidity_attr_templates,
4908faee73fSGuenter Roeck 	[hwmon_fan] = hwmon_fan_attr_templates,
491f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = hwmon_pwm_attr_templates,
492*4413405fSDr. David Alan Gilbert 	[hwmon_intrusion] = hwmon_intrusion_attr_templates,
493d560168bSGuenter Roeck };
494d560168bSGuenter Roeck 
495d560168bSGuenter Roeck static const int __templates_size[] = {
496f4d325d5SGuenter Roeck 	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
497d560168bSGuenter Roeck 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
49800d616cfSGuenter Roeck 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
4999b26947cSGuenter Roeck 	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
500b308f5c7SGuenter Roeck 	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
5016bfcca44SGuenter Roeck 	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
5026bfcca44SGuenter Roeck 	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
5038faee73fSGuenter Roeck 	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
504f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
505*4413405fSDr. David Alan Gilbert 	[hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
506d560168bSGuenter Roeck };
507d560168bSGuenter Roeck 
508d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
509d560168bSGuenter Roeck {
510d560168bSGuenter Roeck 	int i, n;
511d560168bSGuenter Roeck 
512d560168bSGuenter Roeck 	for (i = n = 0; info->config[i]; i++)
513d560168bSGuenter Roeck 		n += hweight32(info->config[i]);
514d560168bSGuenter Roeck 
515d560168bSGuenter Roeck 	return n;
516d560168bSGuenter Roeck }
517d560168bSGuenter Roeck 
5183bf8bdcfSGuenter Roeck static int hwmon_genattrs(const void *drvdata,
519d560168bSGuenter Roeck 			  struct attribute **attrs,
520d560168bSGuenter Roeck 			  const struct hwmon_ops *ops,
521d560168bSGuenter Roeck 			  const struct hwmon_channel_info *info)
522d560168bSGuenter Roeck {
523d560168bSGuenter Roeck 	const char * const *templates;
524d560168bSGuenter Roeck 	int template_size;
525d560168bSGuenter Roeck 	int i, aindex = 0;
526d560168bSGuenter Roeck 
527d560168bSGuenter Roeck 	if (info->type >= ARRAY_SIZE(__templates))
528d560168bSGuenter Roeck 		return -EINVAL;
529d560168bSGuenter Roeck 
530d560168bSGuenter Roeck 	templates = __templates[info->type];
531d560168bSGuenter Roeck 	template_size = __templates_size[info->type];
532d560168bSGuenter Roeck 
533d560168bSGuenter Roeck 	for (i = 0; info->config[i]; i++) {
534d560168bSGuenter Roeck 		u32 attr_mask = info->config[i];
535d560168bSGuenter Roeck 		u32 attr;
536d560168bSGuenter Roeck 
537d560168bSGuenter Roeck 		while (attr_mask) {
538d560168bSGuenter Roeck 			struct attribute *a;
539d560168bSGuenter Roeck 
540d560168bSGuenter Roeck 			attr = __ffs(attr_mask);
541d560168bSGuenter Roeck 			attr_mask &= ~BIT(attr);
542d560168bSGuenter Roeck 			if (attr >= template_size)
543d560168bSGuenter Roeck 				return -EINVAL;
5443bf8bdcfSGuenter Roeck 			a = hwmon_genattr(drvdata, info->type, attr, i,
545d560168bSGuenter Roeck 					  templates[attr], ops);
546d560168bSGuenter Roeck 			if (IS_ERR(a)) {
547d560168bSGuenter Roeck 				if (PTR_ERR(a) != -ENOENT)
548d560168bSGuenter Roeck 					return PTR_ERR(a);
549d560168bSGuenter Roeck 				continue;
550d560168bSGuenter Roeck 			}
551d560168bSGuenter Roeck 			attrs[aindex++] = a;
552d560168bSGuenter Roeck 		}
553d560168bSGuenter Roeck 	}
554d560168bSGuenter Roeck 	return aindex;
555d560168bSGuenter Roeck }
556d560168bSGuenter Roeck 
557d560168bSGuenter Roeck static struct attribute **
5583bf8bdcfSGuenter Roeck __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
559d560168bSGuenter Roeck {
560d560168bSGuenter Roeck 	int ret, i, aindex = 0, nattrs = 0;
561d560168bSGuenter Roeck 	struct attribute **attrs;
562d560168bSGuenter Roeck 
563d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++)
564d560168bSGuenter Roeck 		nattrs += hwmon_num_channel_attrs(chip->info[i]);
565d560168bSGuenter Roeck 
566d560168bSGuenter Roeck 	if (nattrs == 0)
567d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
568d560168bSGuenter Roeck 
5693bf8bdcfSGuenter Roeck 	attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
570d560168bSGuenter Roeck 	if (!attrs)
571d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
572d560168bSGuenter Roeck 
573d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++) {
5743bf8bdcfSGuenter Roeck 		ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
575d560168bSGuenter Roeck 				     chip->info[i]);
5763bf8bdcfSGuenter Roeck 		if (ret < 0) {
5773bf8bdcfSGuenter Roeck 			hwmon_free_attrs(attrs);
578d560168bSGuenter Roeck 			return ERR_PTR(ret);
5793bf8bdcfSGuenter Roeck 		}
580d560168bSGuenter Roeck 		aindex += ret;
581d560168bSGuenter Roeck 	}
582d560168bSGuenter Roeck 
583d560168bSGuenter Roeck 	return attrs;
584d560168bSGuenter Roeck }
585d560168bSGuenter Roeck 
586d560168bSGuenter Roeck static struct device *
587d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
588d560168bSGuenter Roeck 			const struct hwmon_chip_info *chip,
589d560168bSGuenter Roeck 			const struct attribute_group **groups)
590d560168bSGuenter Roeck {
591d560168bSGuenter Roeck 	struct hwmon_device *hwdev;
592d560168bSGuenter Roeck 	struct device *hdev;
593d560168bSGuenter Roeck 	int i, j, err, id;
594d560168bSGuenter Roeck 
59574d3b641SGuenter Roeck 	/* Complain about invalid characters in hwmon name attribute */
596d560168bSGuenter Roeck 	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
59774d3b641SGuenter Roeck 		dev_warn(dev,
59874d3b641SGuenter Roeck 			 "hwmon: '%s' is not a valid name attribute, please fix\n",
59974d3b641SGuenter Roeck 			 name);
600d560168bSGuenter Roeck 
601d560168bSGuenter Roeck 	id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
602d560168bSGuenter Roeck 	if (id < 0)
603d560168bSGuenter Roeck 		return ERR_PTR(id);
604d560168bSGuenter Roeck 
605d560168bSGuenter Roeck 	hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
606d560168bSGuenter Roeck 	if (hwdev == NULL) {
607d560168bSGuenter Roeck 		err = -ENOMEM;
608d560168bSGuenter Roeck 		goto ida_remove;
609d560168bSGuenter Roeck 	}
610d560168bSGuenter Roeck 
611d560168bSGuenter Roeck 	hdev = &hwdev->dev;
612d560168bSGuenter Roeck 
613239552f4SGuenter Roeck 	if (chip) {
614d560168bSGuenter Roeck 		struct attribute **attrs;
615b2a4cc3aSGuenter Roeck 		int ngroups = 2; /* terminating NULL plus &hwdev->groups */
616d560168bSGuenter Roeck 
617d560168bSGuenter Roeck 		if (groups)
618d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
619d560168bSGuenter Roeck 				ngroups++;
620d560168bSGuenter Roeck 
6213bf8bdcfSGuenter Roeck 		hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
62238d8ed65SColin Ian King 		if (!hwdev->groups) {
62338d8ed65SColin Ian King 			err = -ENOMEM;
62438d8ed65SColin Ian King 			goto free_hwmon;
62538d8ed65SColin Ian King 		}
626d560168bSGuenter Roeck 
6273bf8bdcfSGuenter Roeck 		attrs = __hwmon_create_attrs(drvdata, chip);
628d560168bSGuenter Roeck 		if (IS_ERR(attrs)) {
629d560168bSGuenter Roeck 			err = PTR_ERR(attrs);
630d560168bSGuenter Roeck 			goto free_hwmon;
631d560168bSGuenter Roeck 		}
632d560168bSGuenter Roeck 
633d560168bSGuenter Roeck 		hwdev->group.attrs = attrs;
634d560168bSGuenter Roeck 		ngroups = 0;
635d560168bSGuenter Roeck 		hwdev->groups[ngroups++] = &hwdev->group;
636d560168bSGuenter Roeck 
637d560168bSGuenter Roeck 		if (groups) {
638d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
639d560168bSGuenter Roeck 				hwdev->groups[ngroups++] = groups[i];
640d560168bSGuenter Roeck 		}
641d560168bSGuenter Roeck 
642d560168bSGuenter Roeck 		hdev->groups = hwdev->groups;
643d560168bSGuenter Roeck 	} else {
644d560168bSGuenter Roeck 		hdev->groups = groups;
645d560168bSGuenter Roeck 	}
646d560168bSGuenter Roeck 
647d560168bSGuenter Roeck 	hwdev->name = name;
648d560168bSGuenter Roeck 	hdev->class = &hwmon_class;
649d560168bSGuenter Roeck 	hdev->parent = dev;
650d560168bSGuenter Roeck 	hdev->of_node = dev ? dev->of_node : NULL;
651d560168bSGuenter Roeck 	hwdev->chip = chip;
652d560168bSGuenter Roeck 	dev_set_drvdata(hdev, drvdata);
653d560168bSGuenter Roeck 	dev_set_name(hdev, HWMON_ID_FORMAT, id);
654d560168bSGuenter Roeck 	err = device_register(hdev);
655d560168bSGuenter Roeck 	if (err)
656d560168bSGuenter Roeck 		goto free_hwmon;
657d560168bSGuenter Roeck 
658c41dd48eSEduardo Valentin 	if (dev && dev->of_node && chip && chip->ops->read &&
659d560168bSGuenter Roeck 	    chip->info[0]->type == hwmon_chip &&
660d560168bSGuenter Roeck 	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
661d560168bSGuenter Roeck 		const struct hwmon_channel_info **info = chip->info;
662d560168bSGuenter Roeck 
663d560168bSGuenter Roeck 		for (i = 1; info[i]; i++) {
664d560168bSGuenter Roeck 			if (info[i]->type != hwmon_temp)
665d560168bSGuenter Roeck 				continue;
666d560168bSGuenter Roeck 
667d560168bSGuenter Roeck 			for (j = 0; info[i]->config[j]; j++) {
668d560168bSGuenter Roeck 				if (!chip->ops->is_visible(drvdata, hwmon_temp,
669d560168bSGuenter Roeck 							   hwmon_temp_input, j))
670d560168bSGuenter Roeck 					continue;
67147c332deSLinus Walleij 				if (info[i]->config[j] & HWMON_T_INPUT) {
6723bf8bdcfSGuenter Roeck 					err = hwmon_thermal_add_sensor(hdev, j);
67374e35127SDmitry Osipenko 					if (err) {
67474e35127SDmitry Osipenko 						device_unregister(hdev);
675792eac18SGuenter Roeck 						/*
676792eac18SGuenter Roeck 						 * Don't worry about hwdev;
677792eac18SGuenter Roeck 						 * hwmon_dev_release(), called
678792eac18SGuenter Roeck 						 * from device_unregister(),
679792eac18SGuenter Roeck 						 * will free it.
680792eac18SGuenter Roeck 						 */
68174e35127SDmitry Osipenko 						goto ida_remove;
68274e35127SDmitry Osipenko 					}
68347c332deSLinus Walleij 				}
684d560168bSGuenter Roeck 			}
685d560168bSGuenter Roeck 		}
686d560168bSGuenter Roeck 	}
687d560168bSGuenter Roeck 
688d560168bSGuenter Roeck 	return hdev;
689d560168bSGuenter Roeck 
690d560168bSGuenter Roeck free_hwmon:
6913bf8bdcfSGuenter Roeck 	hwmon_dev_release(hdev);
692d560168bSGuenter Roeck ida_remove:
693d560168bSGuenter Roeck 	ida_simple_remove(&hwmon_ida, id);
694d560168bSGuenter Roeck 	return ERR_PTR(err);
695d560168bSGuenter Roeck }
696d560168bSGuenter Roeck 
6971236441fSMark M. Hoffman /**
698bab2243cSGuenter Roeck  * hwmon_device_register_with_groups - register w/ hwmon
699bab2243cSGuenter Roeck  * @dev: the parent device
700bab2243cSGuenter Roeck  * @name: hwmon name attribute
701bab2243cSGuenter Roeck  * @drvdata: driver data to attach to created device
702bab2243cSGuenter Roeck  * @groups: List of attribute groups to create
703bab2243cSGuenter Roeck  *
704bab2243cSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
705bab2243cSGuenter Roeck  * longer needed.
706bab2243cSGuenter Roeck  *
707bab2243cSGuenter Roeck  * Returns the pointer to the new device.
708bab2243cSGuenter Roeck  */
709bab2243cSGuenter Roeck struct device *
710bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name,
711bab2243cSGuenter Roeck 				  void *drvdata,
712bab2243cSGuenter Roeck 				  const struct attribute_group **groups)
713bab2243cSGuenter Roeck {
7148353863aSGuenter Roeck 	if (!name)
7158353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
7168353863aSGuenter Roeck 
717d560168bSGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
718bab2243cSGuenter Roeck }
719bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
720bab2243cSGuenter Roeck 
721bab2243cSGuenter Roeck /**
722d560168bSGuenter Roeck  * hwmon_device_register_with_info - register w/ hwmon
723d560168bSGuenter Roeck  * @dev: the parent device
724d560168bSGuenter Roeck  * @name: hwmon name attribute
725d560168bSGuenter Roeck  * @drvdata: driver data to attach to created device
7263870945aSGuenter Roeck  * @chip: pointer to hwmon chip information
727848ba0a2SGuenter Roeck  * @extra_groups: pointer to list of additional non-standard attribute groups
728d560168bSGuenter Roeck  *
729d560168bSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
730d560168bSGuenter Roeck  * longer needed.
731d560168bSGuenter Roeck  *
732d560168bSGuenter Roeck  * Returns the pointer to the new device.
733d560168bSGuenter Roeck  */
734d560168bSGuenter Roeck struct device *
735d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name,
736d560168bSGuenter Roeck 				void *drvdata,
737d560168bSGuenter Roeck 				const struct hwmon_chip_info *chip,
738848ba0a2SGuenter Roeck 				const struct attribute_group **extra_groups)
739d560168bSGuenter Roeck {
7408353863aSGuenter Roeck 	if (!name)
7418353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
7428353863aSGuenter Roeck 
743239552f4SGuenter Roeck 	if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
744d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
745d560168bSGuenter Roeck 
74659df4f4eSLucas Magasweran 	if (chip && !dev)
74759df4f4eSLucas Magasweran 		return ERR_PTR(-EINVAL);
74859df4f4eSLucas Magasweran 
749848ba0a2SGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
750d560168bSGuenter Roeck }
751d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
752d560168bSGuenter Roeck 
753d560168bSGuenter Roeck /**
7541beeffe4STony Jones  * hwmon_device_register - register w/ hwmon
7551236441fSMark M. Hoffman  * @dev: the device to register
7561236441fSMark M. Hoffman  *
7571beeffe4STony Jones  * hwmon_device_unregister() must be called when the device is no
7581236441fSMark M. Hoffman  * longer needed.
7591236441fSMark M. Hoffman  *
7601beeffe4STony Jones  * Returns the pointer to the new device.
7611236441fSMark M. Hoffman  */
7621beeffe4STony Jones struct device *hwmon_device_register(struct device *dev)
7631236441fSMark M. Hoffman {
764af1bd36cSGuenter Roeck 	dev_warn(dev,
765af1bd36cSGuenter Roeck 		 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
766af1bd36cSGuenter Roeck 
7678353863aSGuenter Roeck 	return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
7681236441fSMark M. Hoffman }
769839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register);
7701236441fSMark M. Hoffman 
7711236441fSMark M. Hoffman /**
7721236441fSMark M. Hoffman  * hwmon_device_unregister - removes the previously registered class device
7731236441fSMark M. Hoffman  *
7741beeffe4STony Jones  * @dev: the class device to destroy
7751236441fSMark M. Hoffman  */
7761beeffe4STony Jones void hwmon_device_unregister(struct device *dev)
7771236441fSMark M. Hoffman {
7781236441fSMark M. Hoffman 	int id;
7791236441fSMark M. Hoffman 
780739cf3a2SKay Sievers 	if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
7811beeffe4STony Jones 		device_unregister(dev);
7824ca5f468SJonathan Cameron 		ida_simple_remove(&hwmon_ida, id);
7831236441fSMark M. Hoffman 	} else
7841beeffe4STony Jones 		dev_dbg(dev->parent,
7851236441fSMark M. Hoffman 			"hwmon_device_unregister() failed: bad class ID!\n");
7861236441fSMark M. Hoffman }
787839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister);
7881236441fSMark M. Hoffman 
78974188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res)
79074188cbaSGuenter Roeck {
79174188cbaSGuenter Roeck 	struct device *hwdev = *(struct device **)res;
79274188cbaSGuenter Roeck 
79374188cbaSGuenter Roeck 	hwmon_device_unregister(hwdev);
79474188cbaSGuenter Roeck }
79574188cbaSGuenter Roeck 
79674188cbaSGuenter Roeck /**
79774188cbaSGuenter Roeck  * devm_hwmon_device_register_with_groups - register w/ hwmon
79874188cbaSGuenter Roeck  * @dev: the parent device
79974188cbaSGuenter Roeck  * @name: hwmon name attribute
80074188cbaSGuenter Roeck  * @drvdata: driver data to attach to created device
80174188cbaSGuenter Roeck  * @groups: List of attribute groups to create
80274188cbaSGuenter Roeck  *
80374188cbaSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
80474188cbaSGuenter Roeck  * unregistered with the parent device.
80574188cbaSGuenter Roeck  */
80674188cbaSGuenter Roeck struct device *
80774188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
80874188cbaSGuenter Roeck 				       void *drvdata,
80974188cbaSGuenter Roeck 				       const struct attribute_group **groups)
81074188cbaSGuenter Roeck {
81174188cbaSGuenter Roeck 	struct device **ptr, *hwdev;
81274188cbaSGuenter Roeck 
81374188cbaSGuenter Roeck 	if (!dev)
81474188cbaSGuenter Roeck 		return ERR_PTR(-EINVAL);
81574188cbaSGuenter Roeck 
81674188cbaSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
81774188cbaSGuenter Roeck 	if (!ptr)
81874188cbaSGuenter Roeck 		return ERR_PTR(-ENOMEM);
81974188cbaSGuenter Roeck 
82074188cbaSGuenter Roeck 	hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
82174188cbaSGuenter Roeck 	if (IS_ERR(hwdev))
82274188cbaSGuenter Roeck 		goto error;
82374188cbaSGuenter Roeck 
82474188cbaSGuenter Roeck 	*ptr = hwdev;
82574188cbaSGuenter Roeck 	devres_add(dev, ptr);
82674188cbaSGuenter Roeck 	return hwdev;
82774188cbaSGuenter Roeck 
82874188cbaSGuenter Roeck error:
82974188cbaSGuenter Roeck 	devres_free(ptr);
83074188cbaSGuenter Roeck 	return hwdev;
83174188cbaSGuenter Roeck }
83274188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
83374188cbaSGuenter Roeck 
834d560168bSGuenter Roeck /**
835d560168bSGuenter Roeck  * devm_hwmon_device_register_with_info - register w/ hwmon
836d560168bSGuenter Roeck  * @dev:	the parent device
837d560168bSGuenter Roeck  * @name:	hwmon name attribute
838d560168bSGuenter Roeck  * @drvdata:	driver data to attach to created device
8393870945aSGuenter Roeck  * @chip:	pointer to hwmon chip information
8403870945aSGuenter Roeck  * @groups:	pointer to list of driver specific attribute groups
841d560168bSGuenter Roeck  *
842d560168bSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
843d560168bSGuenter Roeck  * unregistered with the parent device.
844d560168bSGuenter Roeck  */
845d560168bSGuenter Roeck struct device *
846d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name,
847d560168bSGuenter Roeck 				     void *drvdata,
848d560168bSGuenter Roeck 				     const struct hwmon_chip_info *chip,
849d560168bSGuenter Roeck 				     const struct attribute_group **groups)
850d560168bSGuenter Roeck {
851d560168bSGuenter Roeck 	struct device **ptr, *hwdev;
852d560168bSGuenter Roeck 
853d560168bSGuenter Roeck 	if (!dev)
854d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
855d560168bSGuenter Roeck 
856d560168bSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
857d560168bSGuenter Roeck 	if (!ptr)
858d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
859d560168bSGuenter Roeck 
860d560168bSGuenter Roeck 	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
861d560168bSGuenter Roeck 						groups);
862d560168bSGuenter Roeck 	if (IS_ERR(hwdev))
863d560168bSGuenter Roeck 		goto error;
864d560168bSGuenter Roeck 
865d560168bSGuenter Roeck 	*ptr = hwdev;
866d560168bSGuenter Roeck 	devres_add(dev, ptr);
867d560168bSGuenter Roeck 
868d560168bSGuenter Roeck 	return hwdev;
869d560168bSGuenter Roeck 
870d560168bSGuenter Roeck error:
871d560168bSGuenter Roeck 	devres_free(ptr);
872d560168bSGuenter Roeck 	return hwdev;
873d560168bSGuenter Roeck }
874d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
875d560168bSGuenter Roeck 
87674188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data)
87774188cbaSGuenter Roeck {
87874188cbaSGuenter Roeck 	struct device **hwdev = res;
87974188cbaSGuenter Roeck 
88074188cbaSGuenter Roeck 	return *hwdev == data;
88174188cbaSGuenter Roeck }
88274188cbaSGuenter Roeck 
88374188cbaSGuenter Roeck /**
88474188cbaSGuenter Roeck  * devm_hwmon_device_unregister - removes a previously registered hwmon device
88574188cbaSGuenter Roeck  *
88674188cbaSGuenter Roeck  * @dev: the parent device of the device to unregister
88774188cbaSGuenter Roeck  */
88874188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev)
88974188cbaSGuenter Roeck {
89074188cbaSGuenter Roeck 	WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
89174188cbaSGuenter Roeck }
89274188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
89374188cbaSGuenter Roeck 
8942958b1ecSJean Delvare static void __init hwmon_pci_quirks(void)
8952958b1ecSJean Delvare {
8962958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI
8972958b1ecSJean Delvare 	struct pci_dev *sb;
8982958b1ecSJean Delvare 	u16 base;
8992958b1ecSJean Delvare 	u8 enable;
9002958b1ecSJean Delvare 
9012958b1ecSJean Delvare 	/* Open access to 0x295-0x296 on MSI MS-7031 */
9022958b1ecSJean Delvare 	sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
903d6dab7ddSJean Delvare 	if (sb) {
904d6dab7ddSJean Delvare 		if (sb->subsystem_vendor == 0x1462 &&	/* MSI */
905d6dab7ddSJean Delvare 		    sb->subsystem_device == 0x0031) {	/* MS-7031 */
9062958b1ecSJean Delvare 			pci_read_config_byte(sb, 0x48, &enable);
9072958b1ecSJean Delvare 			pci_read_config_word(sb, 0x64, &base);
9082958b1ecSJean Delvare 
9092958b1ecSJean Delvare 			if (base == 0 && !(enable & BIT(2))) {
9102958b1ecSJean Delvare 				dev_info(&sb->dev,
9112958b1ecSJean Delvare 					 "Opening wide generic port at 0x295\n");
9122958b1ecSJean Delvare 				pci_write_config_word(sb, 0x64, 0x295);
913d6dab7ddSJean Delvare 				pci_write_config_byte(sb, 0x48,
914d6dab7ddSJean Delvare 						      enable | BIT(2));
9152958b1ecSJean Delvare 			}
9162958b1ecSJean Delvare 		}
917d6dab7ddSJean Delvare 		pci_dev_put(sb);
918d6dab7ddSJean Delvare 	}
9192958b1ecSJean Delvare #endif
9202958b1ecSJean Delvare }
9212958b1ecSJean Delvare 
9221236441fSMark M. Hoffman static int __init hwmon_init(void)
9231236441fSMark M. Hoffman {
924bab2243cSGuenter Roeck 	int err;
925bab2243cSGuenter Roeck 
9262958b1ecSJean Delvare 	hwmon_pci_quirks();
9272958b1ecSJean Delvare 
928bab2243cSGuenter Roeck 	err = class_register(&hwmon_class);
929bab2243cSGuenter Roeck 	if (err) {
930bab2243cSGuenter Roeck 		pr_err("couldn't register hwmon sysfs class\n");
931bab2243cSGuenter Roeck 		return err;
9321236441fSMark M. Hoffman 	}
9331236441fSMark M. Hoffman 	return 0;
9341236441fSMark M. Hoffman }
9351236441fSMark M. Hoffman 
9361236441fSMark M. Hoffman static void __exit hwmon_exit(void)
9371236441fSMark M. Hoffman {
938bab2243cSGuenter Roeck 	class_unregister(&hwmon_class);
9391236441fSMark M. Hoffman }
9401236441fSMark M. Hoffman 
94137f54ee5SDavid Brownell subsys_initcall(hwmon_init);
9421236441fSMark M. Hoffman module_exit(hwmon_exit);
9431236441fSMark M. Hoffman 
9441236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
9451236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
9461236441fSMark M. Hoffman MODULE_LICENSE("GPL");
9471236441fSMark M. Hoffman 
948