xref: /linux/drivers/hwmon/hwmon.c (revision 44e3ad882bb268563766c45cd842a229dd3a4902)
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 }
182*44e3ad88SAkinobu Mita 
183*44e3ad88SAkinobu Mita static int hwmon_thermal_register_sensors(struct device *dev)
184*44e3ad88SAkinobu Mita {
185*44e3ad88SAkinobu Mita 	struct hwmon_device *hwdev = to_hwmon_device(dev);
186*44e3ad88SAkinobu Mita 	const struct hwmon_chip_info *chip = hwdev->chip;
187*44e3ad88SAkinobu Mita 	const struct hwmon_channel_info **info = chip->info;
188*44e3ad88SAkinobu Mita 	void *drvdata = dev_get_drvdata(dev);
189*44e3ad88SAkinobu Mita 	int i;
190*44e3ad88SAkinobu Mita 
191*44e3ad88SAkinobu Mita 	for (i = 1; info[i]; i++) {
192*44e3ad88SAkinobu Mita 		int j;
193*44e3ad88SAkinobu Mita 
194*44e3ad88SAkinobu Mita 		if (info[i]->type != hwmon_temp)
195*44e3ad88SAkinobu Mita 			continue;
196*44e3ad88SAkinobu Mita 
197*44e3ad88SAkinobu Mita 		for (j = 0; info[i]->config[j]; j++) {
198*44e3ad88SAkinobu Mita 			int err;
199*44e3ad88SAkinobu Mita 
200*44e3ad88SAkinobu Mita 			if (!(info[i]->config[j] & HWMON_T_INPUT) ||
201*44e3ad88SAkinobu Mita 			    !chip->ops->is_visible(drvdata, hwmon_temp,
202*44e3ad88SAkinobu Mita 						   hwmon_temp_input, j))
203*44e3ad88SAkinobu Mita 				continue;
204*44e3ad88SAkinobu Mita 
205*44e3ad88SAkinobu Mita 			err = hwmon_thermal_add_sensor(dev, j);
206*44e3ad88SAkinobu Mita 			if (err)
207*44e3ad88SAkinobu Mita 				return err;
208*44e3ad88SAkinobu Mita 		}
209*44e3ad88SAkinobu Mita 	}
210*44e3ad88SAkinobu Mita 
211*44e3ad88SAkinobu Mita 	return 0;
212*44e3ad88SAkinobu Mita }
213*44e3ad88SAkinobu Mita 
214d560168bSGuenter Roeck #else
215*44e3ad88SAkinobu Mita static int hwmon_thermal_register_sensors(struct device *dev)
216d560168bSGuenter Roeck {
217d560168bSGuenter Roeck 	return 0;
218d560168bSGuenter Roeck }
21986430c1aSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
220d560168bSGuenter Roeck 
22161b8ab2cSNicolin Chen static int hwmon_attr_base(enum hwmon_sensor_types type)
22261b8ab2cSNicolin Chen {
2234413405fSDr. David Alan Gilbert 	if (type == hwmon_in || type == hwmon_intrusion)
22461b8ab2cSNicolin Chen 		return 0;
22561b8ab2cSNicolin Chen 	return 1;
22661b8ab2cSNicolin Chen }
22761b8ab2cSNicolin Chen 
228d560168bSGuenter Roeck /* sysfs attribute management */
229d560168bSGuenter Roeck 
230d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev,
231d560168bSGuenter Roeck 			       struct device_attribute *devattr, char *buf)
232d560168bSGuenter Roeck {
233d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
234d560168bSGuenter Roeck 	long val;
235d560168bSGuenter Roeck 	int ret;
236d560168bSGuenter Roeck 
237d560168bSGuenter Roeck 	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
238d560168bSGuenter Roeck 			       &val);
239d560168bSGuenter Roeck 	if (ret < 0)
240d560168bSGuenter Roeck 		return ret;
241d560168bSGuenter Roeck 
24261b8ab2cSNicolin Chen 	trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
24361b8ab2cSNicolin Chen 			      hattr->name, val);
24461b8ab2cSNicolin Chen 
245d560168bSGuenter Roeck 	return sprintf(buf, "%ld\n", val);
246d560168bSGuenter Roeck }
247d560168bSGuenter Roeck 
248e159ab5cSGuenter Roeck static ssize_t hwmon_attr_show_string(struct device *dev,
249e159ab5cSGuenter Roeck 				      struct device_attribute *devattr,
250e159ab5cSGuenter Roeck 				      char *buf)
251e159ab5cSGuenter Roeck {
252e159ab5cSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
25361b8ab2cSNicolin Chen 	enum hwmon_sensor_types type = hattr->type;
2545ba6bcbcSJean Delvare 	const char *s;
255e159ab5cSGuenter Roeck 	int ret;
256e159ab5cSGuenter Roeck 
257e159ab5cSGuenter Roeck 	ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
258e159ab5cSGuenter Roeck 				      hattr->index, &s);
259e159ab5cSGuenter Roeck 	if (ret < 0)
260e159ab5cSGuenter Roeck 		return ret;
261e159ab5cSGuenter Roeck 
26261b8ab2cSNicolin Chen 	trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
26361b8ab2cSNicolin Chen 				     hattr->name, s);
26461b8ab2cSNicolin Chen 
265e159ab5cSGuenter Roeck 	return sprintf(buf, "%s\n", s);
266e159ab5cSGuenter Roeck }
267e159ab5cSGuenter Roeck 
268d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev,
269d560168bSGuenter Roeck 				struct device_attribute *devattr,
270d560168bSGuenter Roeck 				const char *buf, size_t count)
271d560168bSGuenter Roeck {
272d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
273d560168bSGuenter Roeck 	long val;
274d560168bSGuenter Roeck 	int ret;
275d560168bSGuenter Roeck 
276d560168bSGuenter Roeck 	ret = kstrtol(buf, 10, &val);
277d560168bSGuenter Roeck 	if (ret < 0)
278d560168bSGuenter Roeck 		return ret;
279d560168bSGuenter Roeck 
280d560168bSGuenter Roeck 	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
281d560168bSGuenter Roeck 				val);
282d560168bSGuenter Roeck 	if (ret < 0)
283d560168bSGuenter Roeck 		return ret;
284d560168bSGuenter Roeck 
28561b8ab2cSNicolin Chen 	trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
28661b8ab2cSNicolin Chen 			       hattr->name, val);
287d560168bSGuenter Roeck 
28861b8ab2cSNicolin Chen 	return count;
289d560168bSGuenter Roeck }
290d560168bSGuenter Roeck 
291e159ab5cSGuenter Roeck static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
292e159ab5cSGuenter Roeck {
293e159ab5cSGuenter Roeck 	return (type == hwmon_temp && attr == hwmon_temp_label) ||
294e159ab5cSGuenter Roeck 	       (type == hwmon_in && attr == hwmon_in_label) ||
295e159ab5cSGuenter Roeck 	       (type == hwmon_curr && attr == hwmon_curr_label) ||
296e159ab5cSGuenter Roeck 	       (type == hwmon_power && attr == hwmon_power_label) ||
297e159ab5cSGuenter Roeck 	       (type == hwmon_energy && attr == hwmon_energy_label) ||
298e159ab5cSGuenter Roeck 	       (type == hwmon_humidity && attr == hwmon_humidity_label) ||
299e159ab5cSGuenter Roeck 	       (type == hwmon_fan && attr == hwmon_fan_label);
300e159ab5cSGuenter Roeck }
301e159ab5cSGuenter Roeck 
3023bf8bdcfSGuenter Roeck static struct attribute *hwmon_genattr(const void *drvdata,
303d560168bSGuenter Roeck 				       enum hwmon_sensor_types type,
304d560168bSGuenter Roeck 				       u32 attr,
305d560168bSGuenter Roeck 				       int index,
306d560168bSGuenter Roeck 				       const char *template,
307d560168bSGuenter Roeck 				       const struct hwmon_ops *ops)
308d560168bSGuenter Roeck {
309d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr;
310d560168bSGuenter Roeck 	struct device_attribute *dattr;
311d560168bSGuenter Roeck 	struct attribute *a;
312d560168bSGuenter Roeck 	umode_t mode;
3133b443defSRasmus Villemoes 	const char *name;
314e159ab5cSGuenter Roeck 	bool is_string = is_string_attr(type, attr);
315d560168bSGuenter Roeck 
316d560168bSGuenter Roeck 	/* The attribute is invisible if there is no template string */
317d560168bSGuenter Roeck 	if (!template)
318d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
319d560168bSGuenter Roeck 
320d560168bSGuenter Roeck 	mode = ops->is_visible(drvdata, type, attr, index);
321d560168bSGuenter Roeck 	if (!mode)
322d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
323d560168bSGuenter Roeck 
3240d87116fSGuenter Roeck 	if ((mode & 0444) && ((is_string && !ops->read_string) ||
325e159ab5cSGuenter Roeck 				 (!is_string && !ops->read)))
326d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
3270d87116fSGuenter Roeck 	if ((mode & 0222) && !ops->write)
328d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
329d560168bSGuenter Roeck 
3303bf8bdcfSGuenter Roeck 	hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
331d560168bSGuenter Roeck 	if (!hattr)
332d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
333d560168bSGuenter Roeck 
3343a412d5eSGuenter Roeck 	if (type == hwmon_chip) {
3353b443defSRasmus Villemoes 		name = template;
3363a412d5eSGuenter Roeck 	} else {
3373a412d5eSGuenter Roeck 		scnprintf(hattr->name, sizeof(hattr->name), template,
3383a412d5eSGuenter Roeck 			  index + hwmon_attr_base(type));
3393a412d5eSGuenter Roeck 		name = hattr->name;
3403a412d5eSGuenter Roeck 	}
3413a412d5eSGuenter Roeck 
342d560168bSGuenter Roeck 	hattr->type = type;
343d560168bSGuenter Roeck 	hattr->attr = attr;
344d560168bSGuenter Roeck 	hattr->index = index;
345d560168bSGuenter Roeck 	hattr->ops = ops;
346d560168bSGuenter Roeck 
347d560168bSGuenter Roeck 	dattr = &hattr->dev_attr;
348e159ab5cSGuenter Roeck 	dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
349d560168bSGuenter Roeck 	dattr->store = hwmon_attr_store;
350d560168bSGuenter Roeck 
351d560168bSGuenter Roeck 	a = &dattr->attr;
352d560168bSGuenter Roeck 	sysfs_attr_init(a);
353d560168bSGuenter Roeck 	a->name = name;
354d560168bSGuenter Roeck 	a->mode = mode;
355d560168bSGuenter Roeck 
356d560168bSGuenter Roeck 	return a;
357d560168bSGuenter Roeck }
358d560168bSGuenter Roeck 
359f4d325d5SGuenter Roeck /*
360f4d325d5SGuenter Roeck  * Chip attributes are not attribute templates but actual sysfs attributes.
361f4d325d5SGuenter Roeck  * See hwmon_genattr() for special handling.
362f4d325d5SGuenter Roeck  */
363f4d325d5SGuenter Roeck static const char * const hwmon_chip_attrs[] = {
364d560168bSGuenter Roeck 	[hwmon_chip_temp_reset_history] = "temp_reset_history",
36500d616cfSGuenter Roeck 	[hwmon_chip_in_reset_history] = "in_reset_history",
3669b26947cSGuenter Roeck 	[hwmon_chip_curr_reset_history] = "curr_reset_history",
367b308f5c7SGuenter Roeck 	[hwmon_chip_power_reset_history] = "power_reset_history",
368d560168bSGuenter Roeck 	[hwmon_chip_update_interval] = "update_interval",
369d560168bSGuenter Roeck 	[hwmon_chip_alarms] = "alarms",
3709f00995eSGuenter Roeck 	[hwmon_chip_samples] = "samples",
3719f00995eSGuenter Roeck 	[hwmon_chip_curr_samples] = "curr_samples",
3729f00995eSGuenter Roeck 	[hwmon_chip_in_samples] = "in_samples",
3739f00995eSGuenter Roeck 	[hwmon_chip_power_samples] = "power_samples",
3749f00995eSGuenter Roeck 	[hwmon_chip_temp_samples] = "temp_samples",
375d560168bSGuenter Roeck };
376d560168bSGuenter Roeck 
377d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = {
378002c6b54SGuenter Roeck 	[hwmon_temp_enable] = "temp%d_enable",
379d560168bSGuenter Roeck 	[hwmon_temp_input] = "temp%d_input",
380d560168bSGuenter Roeck 	[hwmon_temp_type] = "temp%d_type",
381d560168bSGuenter Roeck 	[hwmon_temp_lcrit] = "temp%d_lcrit",
382d560168bSGuenter Roeck 	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
383d560168bSGuenter Roeck 	[hwmon_temp_min] = "temp%d_min",
384d560168bSGuenter Roeck 	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
385d560168bSGuenter Roeck 	[hwmon_temp_max] = "temp%d_max",
386d560168bSGuenter Roeck 	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
387d560168bSGuenter Roeck 	[hwmon_temp_crit] = "temp%d_crit",
388d560168bSGuenter Roeck 	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
389d560168bSGuenter Roeck 	[hwmon_temp_emergency] = "temp%d_emergency",
390d560168bSGuenter Roeck 	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
391d560168bSGuenter Roeck 	[hwmon_temp_alarm] = "temp%d_alarm",
392d560168bSGuenter Roeck 	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
393d560168bSGuenter Roeck 	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
394d560168bSGuenter Roeck 	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
395d560168bSGuenter Roeck 	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
396d560168bSGuenter Roeck 	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
397d560168bSGuenter Roeck 	[hwmon_temp_fault] = "temp%d_fault",
398d560168bSGuenter Roeck 	[hwmon_temp_offset] = "temp%d_offset",
399d560168bSGuenter Roeck 	[hwmon_temp_label] = "temp%d_label",
400d560168bSGuenter Roeck 	[hwmon_temp_lowest] = "temp%d_lowest",
401d560168bSGuenter Roeck 	[hwmon_temp_highest] = "temp%d_highest",
402d560168bSGuenter Roeck 	[hwmon_temp_reset_history] = "temp%d_reset_history",
403d560168bSGuenter Roeck };
404d560168bSGuenter Roeck 
40500d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = {
406002c6b54SGuenter Roeck 	[hwmon_in_enable] = "in%d_enable",
40700d616cfSGuenter Roeck 	[hwmon_in_input] = "in%d_input",
40800d616cfSGuenter Roeck 	[hwmon_in_min] = "in%d_min",
40900d616cfSGuenter Roeck 	[hwmon_in_max] = "in%d_max",
41000d616cfSGuenter Roeck 	[hwmon_in_lcrit] = "in%d_lcrit",
41100d616cfSGuenter Roeck 	[hwmon_in_crit] = "in%d_crit",
41200d616cfSGuenter Roeck 	[hwmon_in_average] = "in%d_average",
41300d616cfSGuenter Roeck 	[hwmon_in_lowest] = "in%d_lowest",
41400d616cfSGuenter Roeck 	[hwmon_in_highest] = "in%d_highest",
41500d616cfSGuenter Roeck 	[hwmon_in_reset_history] = "in%d_reset_history",
41600d616cfSGuenter Roeck 	[hwmon_in_label] = "in%d_label",
41700d616cfSGuenter Roeck 	[hwmon_in_alarm] = "in%d_alarm",
41800d616cfSGuenter Roeck 	[hwmon_in_min_alarm] = "in%d_min_alarm",
41900d616cfSGuenter Roeck 	[hwmon_in_max_alarm] = "in%d_max_alarm",
42000d616cfSGuenter Roeck 	[hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
42100d616cfSGuenter Roeck 	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
42200d616cfSGuenter Roeck };
42300d616cfSGuenter Roeck 
4249b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = {
425002c6b54SGuenter Roeck 	[hwmon_curr_enable] = "curr%d_enable",
4269b26947cSGuenter Roeck 	[hwmon_curr_input] = "curr%d_input",
4279b26947cSGuenter Roeck 	[hwmon_curr_min] = "curr%d_min",
4289b26947cSGuenter Roeck 	[hwmon_curr_max] = "curr%d_max",
4299b26947cSGuenter Roeck 	[hwmon_curr_lcrit] = "curr%d_lcrit",
4309b26947cSGuenter Roeck 	[hwmon_curr_crit] = "curr%d_crit",
4319b26947cSGuenter Roeck 	[hwmon_curr_average] = "curr%d_average",
4329b26947cSGuenter Roeck 	[hwmon_curr_lowest] = "curr%d_lowest",
4339b26947cSGuenter Roeck 	[hwmon_curr_highest] = "curr%d_highest",
4349b26947cSGuenter Roeck 	[hwmon_curr_reset_history] = "curr%d_reset_history",
4359b26947cSGuenter Roeck 	[hwmon_curr_label] = "curr%d_label",
4369b26947cSGuenter Roeck 	[hwmon_curr_alarm] = "curr%d_alarm",
4379b26947cSGuenter Roeck 	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
4389b26947cSGuenter Roeck 	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
4399b26947cSGuenter Roeck 	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
4409b26947cSGuenter Roeck 	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
4419b26947cSGuenter Roeck };
4429b26947cSGuenter Roeck 
443b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = {
444002c6b54SGuenter Roeck 	[hwmon_power_enable] = "power%d_enable",
445b308f5c7SGuenter Roeck 	[hwmon_power_average] = "power%d_average",
446b308f5c7SGuenter Roeck 	[hwmon_power_average_interval] = "power%d_average_interval",
447b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_max] = "power%d_interval_max",
448b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_min] = "power%d_interval_min",
449b308f5c7SGuenter Roeck 	[hwmon_power_average_highest] = "power%d_average_highest",
450b308f5c7SGuenter Roeck 	[hwmon_power_average_lowest] = "power%d_average_lowest",
451b308f5c7SGuenter Roeck 	[hwmon_power_average_max] = "power%d_average_max",
452b308f5c7SGuenter Roeck 	[hwmon_power_average_min] = "power%d_average_min",
453b308f5c7SGuenter Roeck 	[hwmon_power_input] = "power%d_input",
454b308f5c7SGuenter Roeck 	[hwmon_power_input_highest] = "power%d_input_highest",
455b308f5c7SGuenter Roeck 	[hwmon_power_input_lowest] = "power%d_input_lowest",
456b308f5c7SGuenter Roeck 	[hwmon_power_reset_history] = "power%d_reset_history",
457b308f5c7SGuenter Roeck 	[hwmon_power_accuracy] = "power%d_accuracy",
458b308f5c7SGuenter Roeck 	[hwmon_power_cap] = "power%d_cap",
459b308f5c7SGuenter Roeck 	[hwmon_power_cap_hyst] = "power%d_cap_hyst",
460b308f5c7SGuenter Roeck 	[hwmon_power_cap_max] = "power%d_cap_max",
461b308f5c7SGuenter Roeck 	[hwmon_power_cap_min] = "power%d_cap_min",
462aa7f29b0SAndrew Lunn 	[hwmon_power_min] = "power%d_min",
463b308f5c7SGuenter Roeck 	[hwmon_power_max] = "power%d_max",
464aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit] = "power%d_lcrit",
465b308f5c7SGuenter Roeck 	[hwmon_power_crit] = "power%d_crit",
466b308f5c7SGuenter Roeck 	[hwmon_power_label] = "power%d_label",
467b308f5c7SGuenter Roeck 	[hwmon_power_alarm] = "power%d_alarm",
468b308f5c7SGuenter Roeck 	[hwmon_power_cap_alarm] = "power%d_cap_alarm",
469aa7f29b0SAndrew Lunn 	[hwmon_power_min_alarm] = "power%d_min_alarm",
470b308f5c7SGuenter Roeck 	[hwmon_power_max_alarm] = "power%d_max_alarm",
471aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
472b308f5c7SGuenter Roeck 	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
473b308f5c7SGuenter Roeck };
474b308f5c7SGuenter Roeck 
4756bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = {
476002c6b54SGuenter Roeck 	[hwmon_energy_enable] = "energy%d_enable",
4776bfcca44SGuenter Roeck 	[hwmon_energy_input] = "energy%d_input",
4786bfcca44SGuenter Roeck 	[hwmon_energy_label] = "energy%d_label",
4796bfcca44SGuenter Roeck };
4806bfcca44SGuenter Roeck 
4816bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = {
482002c6b54SGuenter Roeck 	[hwmon_humidity_enable] = "humidity%d_enable",
4836bfcca44SGuenter Roeck 	[hwmon_humidity_input] = "humidity%d_input",
4846bfcca44SGuenter Roeck 	[hwmon_humidity_label] = "humidity%d_label",
4856bfcca44SGuenter Roeck 	[hwmon_humidity_min] = "humidity%d_min",
4866bfcca44SGuenter Roeck 	[hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
4876bfcca44SGuenter Roeck 	[hwmon_humidity_max] = "humidity%d_max",
4886bfcca44SGuenter Roeck 	[hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
4896bfcca44SGuenter Roeck 	[hwmon_humidity_alarm] = "humidity%d_alarm",
4906bfcca44SGuenter Roeck 	[hwmon_humidity_fault] = "humidity%d_fault",
4916bfcca44SGuenter Roeck };
4926bfcca44SGuenter Roeck 
4938faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = {
494002c6b54SGuenter Roeck 	[hwmon_fan_enable] = "fan%d_enable",
4958faee73fSGuenter Roeck 	[hwmon_fan_input] = "fan%d_input",
4968faee73fSGuenter Roeck 	[hwmon_fan_label] = "fan%d_label",
4978faee73fSGuenter Roeck 	[hwmon_fan_min] = "fan%d_min",
4988faee73fSGuenter Roeck 	[hwmon_fan_max] = "fan%d_max",
4998faee73fSGuenter Roeck 	[hwmon_fan_div] = "fan%d_div",
5008faee73fSGuenter Roeck 	[hwmon_fan_pulses] = "fan%d_pulses",
5018faee73fSGuenter Roeck 	[hwmon_fan_target] = "fan%d_target",
5028faee73fSGuenter Roeck 	[hwmon_fan_alarm] = "fan%d_alarm",
5038faee73fSGuenter Roeck 	[hwmon_fan_min_alarm] = "fan%d_min_alarm",
5048faee73fSGuenter Roeck 	[hwmon_fan_max_alarm] = "fan%d_max_alarm",
5058faee73fSGuenter Roeck 	[hwmon_fan_fault] = "fan%d_fault",
5068faee73fSGuenter Roeck };
5078faee73fSGuenter Roeck 
508f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = {
509f9f7bb3aSGuenter Roeck 	[hwmon_pwm_input] = "pwm%d",
510f9f7bb3aSGuenter Roeck 	[hwmon_pwm_enable] = "pwm%d_enable",
511f9f7bb3aSGuenter Roeck 	[hwmon_pwm_mode] = "pwm%d_mode",
512f9f7bb3aSGuenter Roeck 	[hwmon_pwm_freq] = "pwm%d_freq",
513f9f7bb3aSGuenter Roeck };
514f9f7bb3aSGuenter Roeck 
5154413405fSDr. David Alan Gilbert static const char * const hwmon_intrusion_attr_templates[] = {
5164413405fSDr. David Alan Gilbert 	[hwmon_intrusion_alarm] = "intrusion%d_alarm",
5174413405fSDr. David Alan Gilbert 	[hwmon_intrusion_beep]  = "intrusion%d_beep",
5184413405fSDr. David Alan Gilbert };
5194413405fSDr. David Alan Gilbert 
520d560168bSGuenter Roeck static const char * const *__templates[] = {
521f4d325d5SGuenter Roeck 	[hwmon_chip] = hwmon_chip_attrs,
522d560168bSGuenter Roeck 	[hwmon_temp] = hwmon_temp_attr_templates,
52300d616cfSGuenter Roeck 	[hwmon_in] = hwmon_in_attr_templates,
5249b26947cSGuenter Roeck 	[hwmon_curr] = hwmon_curr_attr_templates,
525b308f5c7SGuenter Roeck 	[hwmon_power] = hwmon_power_attr_templates,
5266bfcca44SGuenter Roeck 	[hwmon_energy] = hwmon_energy_attr_templates,
5276bfcca44SGuenter Roeck 	[hwmon_humidity] = hwmon_humidity_attr_templates,
5288faee73fSGuenter Roeck 	[hwmon_fan] = hwmon_fan_attr_templates,
529f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = hwmon_pwm_attr_templates,
5304413405fSDr. David Alan Gilbert 	[hwmon_intrusion] = hwmon_intrusion_attr_templates,
531d560168bSGuenter Roeck };
532d560168bSGuenter Roeck 
533d560168bSGuenter Roeck static const int __templates_size[] = {
534f4d325d5SGuenter Roeck 	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
535d560168bSGuenter Roeck 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
53600d616cfSGuenter Roeck 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
5379b26947cSGuenter Roeck 	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
538b308f5c7SGuenter Roeck 	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
5396bfcca44SGuenter Roeck 	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
5406bfcca44SGuenter Roeck 	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
5418faee73fSGuenter Roeck 	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
542f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
5434413405fSDr. David Alan Gilbert 	[hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
544d560168bSGuenter Roeck };
545d560168bSGuenter Roeck 
546d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
547d560168bSGuenter Roeck {
548d560168bSGuenter Roeck 	int i, n;
549d560168bSGuenter Roeck 
550d560168bSGuenter Roeck 	for (i = n = 0; info->config[i]; i++)
551d560168bSGuenter Roeck 		n += hweight32(info->config[i]);
552d560168bSGuenter Roeck 
553d560168bSGuenter Roeck 	return n;
554d560168bSGuenter Roeck }
555d560168bSGuenter Roeck 
5563bf8bdcfSGuenter Roeck static int hwmon_genattrs(const void *drvdata,
557d560168bSGuenter Roeck 			  struct attribute **attrs,
558d560168bSGuenter Roeck 			  const struct hwmon_ops *ops,
559d560168bSGuenter Roeck 			  const struct hwmon_channel_info *info)
560d560168bSGuenter Roeck {
561d560168bSGuenter Roeck 	const char * const *templates;
562d560168bSGuenter Roeck 	int template_size;
563d560168bSGuenter Roeck 	int i, aindex = 0;
564d560168bSGuenter Roeck 
565d560168bSGuenter Roeck 	if (info->type >= ARRAY_SIZE(__templates))
566d560168bSGuenter Roeck 		return -EINVAL;
567d560168bSGuenter Roeck 
568d560168bSGuenter Roeck 	templates = __templates[info->type];
569d560168bSGuenter Roeck 	template_size = __templates_size[info->type];
570d560168bSGuenter Roeck 
571d560168bSGuenter Roeck 	for (i = 0; info->config[i]; i++) {
572d560168bSGuenter Roeck 		u32 attr_mask = info->config[i];
573d560168bSGuenter Roeck 		u32 attr;
574d560168bSGuenter Roeck 
575d560168bSGuenter Roeck 		while (attr_mask) {
576d560168bSGuenter Roeck 			struct attribute *a;
577d560168bSGuenter Roeck 
578d560168bSGuenter Roeck 			attr = __ffs(attr_mask);
579d560168bSGuenter Roeck 			attr_mask &= ~BIT(attr);
580d560168bSGuenter Roeck 			if (attr >= template_size)
581d560168bSGuenter Roeck 				return -EINVAL;
5823bf8bdcfSGuenter Roeck 			a = hwmon_genattr(drvdata, info->type, attr, i,
583d560168bSGuenter Roeck 					  templates[attr], ops);
584d560168bSGuenter Roeck 			if (IS_ERR(a)) {
585d560168bSGuenter Roeck 				if (PTR_ERR(a) != -ENOENT)
586d560168bSGuenter Roeck 					return PTR_ERR(a);
587d560168bSGuenter Roeck 				continue;
588d560168bSGuenter Roeck 			}
589d560168bSGuenter Roeck 			attrs[aindex++] = a;
590d560168bSGuenter Roeck 		}
591d560168bSGuenter Roeck 	}
592d560168bSGuenter Roeck 	return aindex;
593d560168bSGuenter Roeck }
594d560168bSGuenter Roeck 
595d560168bSGuenter Roeck static struct attribute **
5963bf8bdcfSGuenter Roeck __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
597d560168bSGuenter Roeck {
598d560168bSGuenter Roeck 	int ret, i, aindex = 0, nattrs = 0;
599d560168bSGuenter Roeck 	struct attribute **attrs;
600d560168bSGuenter Roeck 
601d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++)
602d560168bSGuenter Roeck 		nattrs += hwmon_num_channel_attrs(chip->info[i]);
603d560168bSGuenter Roeck 
604d560168bSGuenter Roeck 	if (nattrs == 0)
605d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
606d560168bSGuenter Roeck 
6073bf8bdcfSGuenter Roeck 	attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
608d560168bSGuenter Roeck 	if (!attrs)
609d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
610d560168bSGuenter Roeck 
611d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++) {
6123bf8bdcfSGuenter Roeck 		ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
613d560168bSGuenter Roeck 				     chip->info[i]);
6143bf8bdcfSGuenter Roeck 		if (ret < 0) {
6153bf8bdcfSGuenter Roeck 			hwmon_free_attrs(attrs);
616d560168bSGuenter Roeck 			return ERR_PTR(ret);
6173bf8bdcfSGuenter Roeck 		}
618d560168bSGuenter Roeck 		aindex += ret;
619d560168bSGuenter Roeck 	}
620d560168bSGuenter Roeck 
621d560168bSGuenter Roeck 	return attrs;
622d560168bSGuenter Roeck }
623d560168bSGuenter Roeck 
624d560168bSGuenter Roeck static struct device *
625d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
626d560168bSGuenter Roeck 			const struct hwmon_chip_info *chip,
627d560168bSGuenter Roeck 			const struct attribute_group **groups)
628d560168bSGuenter Roeck {
629d560168bSGuenter Roeck 	struct hwmon_device *hwdev;
630d560168bSGuenter Roeck 	struct device *hdev;
631*44e3ad88SAkinobu Mita 	int i, err, id;
632d560168bSGuenter Roeck 
63374d3b641SGuenter Roeck 	/* Complain about invalid characters in hwmon name attribute */
634d560168bSGuenter Roeck 	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
63574d3b641SGuenter Roeck 		dev_warn(dev,
63674d3b641SGuenter Roeck 			 "hwmon: '%s' is not a valid name attribute, please fix\n",
63774d3b641SGuenter Roeck 			 name);
638d560168bSGuenter Roeck 
639d560168bSGuenter Roeck 	id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
640d560168bSGuenter Roeck 	if (id < 0)
641d560168bSGuenter Roeck 		return ERR_PTR(id);
642d560168bSGuenter Roeck 
643d560168bSGuenter Roeck 	hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
644d560168bSGuenter Roeck 	if (hwdev == NULL) {
645d560168bSGuenter Roeck 		err = -ENOMEM;
646d560168bSGuenter Roeck 		goto ida_remove;
647d560168bSGuenter Roeck 	}
648d560168bSGuenter Roeck 
649d560168bSGuenter Roeck 	hdev = &hwdev->dev;
650d560168bSGuenter Roeck 
651239552f4SGuenter Roeck 	if (chip) {
652d560168bSGuenter Roeck 		struct attribute **attrs;
653b2a4cc3aSGuenter Roeck 		int ngroups = 2; /* terminating NULL plus &hwdev->groups */
654d560168bSGuenter Roeck 
655d560168bSGuenter Roeck 		if (groups)
656d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
657d560168bSGuenter Roeck 				ngroups++;
658d560168bSGuenter Roeck 
6593bf8bdcfSGuenter Roeck 		hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
66038d8ed65SColin Ian King 		if (!hwdev->groups) {
66138d8ed65SColin Ian King 			err = -ENOMEM;
66238d8ed65SColin Ian King 			goto free_hwmon;
66338d8ed65SColin Ian King 		}
664d560168bSGuenter Roeck 
6653bf8bdcfSGuenter Roeck 		attrs = __hwmon_create_attrs(drvdata, chip);
666d560168bSGuenter Roeck 		if (IS_ERR(attrs)) {
667d560168bSGuenter Roeck 			err = PTR_ERR(attrs);
668d560168bSGuenter Roeck 			goto free_hwmon;
669d560168bSGuenter Roeck 		}
670d560168bSGuenter Roeck 
671d560168bSGuenter Roeck 		hwdev->group.attrs = attrs;
672d560168bSGuenter Roeck 		ngroups = 0;
673d560168bSGuenter Roeck 		hwdev->groups[ngroups++] = &hwdev->group;
674d560168bSGuenter Roeck 
675d560168bSGuenter Roeck 		if (groups) {
676d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
677d560168bSGuenter Roeck 				hwdev->groups[ngroups++] = groups[i];
678d560168bSGuenter Roeck 		}
679d560168bSGuenter Roeck 
680d560168bSGuenter Roeck 		hdev->groups = hwdev->groups;
681d560168bSGuenter Roeck 	} else {
682d560168bSGuenter Roeck 		hdev->groups = groups;
683d560168bSGuenter Roeck 	}
684d560168bSGuenter Roeck 
685d560168bSGuenter Roeck 	hwdev->name = name;
686d560168bSGuenter Roeck 	hdev->class = &hwmon_class;
687d560168bSGuenter Roeck 	hdev->parent = dev;
688d560168bSGuenter Roeck 	hdev->of_node = dev ? dev->of_node : NULL;
689d560168bSGuenter Roeck 	hwdev->chip = chip;
690d560168bSGuenter Roeck 	dev_set_drvdata(hdev, drvdata);
691d560168bSGuenter Roeck 	dev_set_name(hdev, HWMON_ID_FORMAT, id);
692d560168bSGuenter Roeck 	err = device_register(hdev);
693d560168bSGuenter Roeck 	if (err)
694d560168bSGuenter Roeck 		goto free_hwmon;
695d560168bSGuenter Roeck 
696c41dd48eSEduardo Valentin 	if (dev && dev->of_node && chip && chip->ops->read &&
697d560168bSGuenter Roeck 	    chip->info[0]->type == hwmon_chip &&
698d560168bSGuenter Roeck 	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
699*44e3ad88SAkinobu Mita 		err = hwmon_thermal_register_sensors(hdev);
70074e35127SDmitry Osipenko 		if (err) {
70174e35127SDmitry Osipenko 			device_unregister(hdev);
702792eac18SGuenter Roeck 			/*
703*44e3ad88SAkinobu Mita 			 * Don't worry about hwdev; hwmon_dev_release(), called
704*44e3ad88SAkinobu Mita 			 * from device_unregister(), will free it.
705792eac18SGuenter Roeck 			 */
70674e35127SDmitry Osipenko 			goto ida_remove;
70774e35127SDmitry Osipenko 		}
70847c332deSLinus Walleij 	}
709d560168bSGuenter Roeck 
710d560168bSGuenter Roeck 	return hdev;
711d560168bSGuenter Roeck 
712d560168bSGuenter Roeck free_hwmon:
7133bf8bdcfSGuenter Roeck 	hwmon_dev_release(hdev);
714d560168bSGuenter Roeck ida_remove:
715d560168bSGuenter Roeck 	ida_simple_remove(&hwmon_ida, id);
716d560168bSGuenter Roeck 	return ERR_PTR(err);
717d560168bSGuenter Roeck }
718d560168bSGuenter Roeck 
7191236441fSMark M. Hoffman /**
720bab2243cSGuenter Roeck  * hwmon_device_register_with_groups - register w/ hwmon
721bab2243cSGuenter Roeck  * @dev: the parent device
722bab2243cSGuenter Roeck  * @name: hwmon name attribute
723bab2243cSGuenter Roeck  * @drvdata: driver data to attach to created device
724bab2243cSGuenter Roeck  * @groups: List of attribute groups to create
725bab2243cSGuenter Roeck  *
726bab2243cSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
727bab2243cSGuenter Roeck  * longer needed.
728bab2243cSGuenter Roeck  *
729bab2243cSGuenter Roeck  * Returns the pointer to the new device.
730bab2243cSGuenter Roeck  */
731bab2243cSGuenter Roeck struct device *
732bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name,
733bab2243cSGuenter Roeck 				  void *drvdata,
734bab2243cSGuenter Roeck 				  const struct attribute_group **groups)
735bab2243cSGuenter Roeck {
7368353863aSGuenter Roeck 	if (!name)
7378353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
7388353863aSGuenter Roeck 
739d560168bSGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
740bab2243cSGuenter Roeck }
741bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
742bab2243cSGuenter Roeck 
743bab2243cSGuenter Roeck /**
744d560168bSGuenter Roeck  * hwmon_device_register_with_info - register w/ hwmon
745d560168bSGuenter Roeck  * @dev: the parent device
746d560168bSGuenter Roeck  * @name: hwmon name attribute
747d560168bSGuenter Roeck  * @drvdata: driver data to attach to created device
7483870945aSGuenter Roeck  * @chip: pointer to hwmon chip information
749848ba0a2SGuenter Roeck  * @extra_groups: pointer to list of additional non-standard attribute groups
750d560168bSGuenter Roeck  *
751d560168bSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
752d560168bSGuenter Roeck  * longer needed.
753d560168bSGuenter Roeck  *
754d560168bSGuenter Roeck  * Returns the pointer to the new device.
755d560168bSGuenter Roeck  */
756d560168bSGuenter Roeck struct device *
757d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name,
758d560168bSGuenter Roeck 				void *drvdata,
759d560168bSGuenter Roeck 				const struct hwmon_chip_info *chip,
760848ba0a2SGuenter Roeck 				const struct attribute_group **extra_groups)
761d560168bSGuenter Roeck {
7628353863aSGuenter Roeck 	if (!name)
7638353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
7648353863aSGuenter Roeck 
765239552f4SGuenter Roeck 	if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
766d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
767d560168bSGuenter Roeck 
76859df4f4eSLucas Magasweran 	if (chip && !dev)
76959df4f4eSLucas Magasweran 		return ERR_PTR(-EINVAL);
77059df4f4eSLucas Magasweran 
771848ba0a2SGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
772d560168bSGuenter Roeck }
773d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
774d560168bSGuenter Roeck 
775d560168bSGuenter Roeck /**
7761beeffe4STony Jones  * hwmon_device_register - register w/ hwmon
7771236441fSMark M. Hoffman  * @dev: the device to register
7781236441fSMark M. Hoffman  *
7791beeffe4STony Jones  * hwmon_device_unregister() must be called when the device is no
7801236441fSMark M. Hoffman  * longer needed.
7811236441fSMark M. Hoffman  *
7821beeffe4STony Jones  * Returns the pointer to the new device.
7831236441fSMark M. Hoffman  */
7841beeffe4STony Jones struct device *hwmon_device_register(struct device *dev)
7851236441fSMark M. Hoffman {
786af1bd36cSGuenter Roeck 	dev_warn(dev,
787af1bd36cSGuenter Roeck 		 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
788af1bd36cSGuenter Roeck 
7898353863aSGuenter Roeck 	return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
7901236441fSMark M. Hoffman }
791839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register);
7921236441fSMark M. Hoffman 
7931236441fSMark M. Hoffman /**
7941236441fSMark M. Hoffman  * hwmon_device_unregister - removes the previously registered class device
7951236441fSMark M. Hoffman  *
7961beeffe4STony Jones  * @dev: the class device to destroy
7971236441fSMark M. Hoffman  */
7981beeffe4STony Jones void hwmon_device_unregister(struct device *dev)
7991236441fSMark M. Hoffman {
8001236441fSMark M. Hoffman 	int id;
8011236441fSMark M. Hoffman 
802739cf3a2SKay Sievers 	if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
8031beeffe4STony Jones 		device_unregister(dev);
8044ca5f468SJonathan Cameron 		ida_simple_remove(&hwmon_ida, id);
8051236441fSMark M. Hoffman 	} else
8061beeffe4STony Jones 		dev_dbg(dev->parent,
8071236441fSMark M. Hoffman 			"hwmon_device_unregister() failed: bad class ID!\n");
8081236441fSMark M. Hoffman }
809839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister);
8101236441fSMark M. Hoffman 
81174188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res)
81274188cbaSGuenter Roeck {
81374188cbaSGuenter Roeck 	struct device *hwdev = *(struct device **)res;
81474188cbaSGuenter Roeck 
81574188cbaSGuenter Roeck 	hwmon_device_unregister(hwdev);
81674188cbaSGuenter Roeck }
81774188cbaSGuenter Roeck 
81874188cbaSGuenter Roeck /**
81974188cbaSGuenter Roeck  * devm_hwmon_device_register_with_groups - register w/ hwmon
82074188cbaSGuenter Roeck  * @dev: the parent device
82174188cbaSGuenter Roeck  * @name: hwmon name attribute
82274188cbaSGuenter Roeck  * @drvdata: driver data to attach to created device
82374188cbaSGuenter Roeck  * @groups: List of attribute groups to create
82474188cbaSGuenter Roeck  *
82574188cbaSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
82674188cbaSGuenter Roeck  * unregistered with the parent device.
82774188cbaSGuenter Roeck  */
82874188cbaSGuenter Roeck struct device *
82974188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
83074188cbaSGuenter Roeck 				       void *drvdata,
83174188cbaSGuenter Roeck 				       const struct attribute_group **groups)
83274188cbaSGuenter Roeck {
83374188cbaSGuenter Roeck 	struct device **ptr, *hwdev;
83474188cbaSGuenter Roeck 
83574188cbaSGuenter Roeck 	if (!dev)
83674188cbaSGuenter Roeck 		return ERR_PTR(-EINVAL);
83774188cbaSGuenter Roeck 
83874188cbaSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
83974188cbaSGuenter Roeck 	if (!ptr)
84074188cbaSGuenter Roeck 		return ERR_PTR(-ENOMEM);
84174188cbaSGuenter Roeck 
84274188cbaSGuenter Roeck 	hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
84374188cbaSGuenter Roeck 	if (IS_ERR(hwdev))
84474188cbaSGuenter Roeck 		goto error;
84574188cbaSGuenter Roeck 
84674188cbaSGuenter Roeck 	*ptr = hwdev;
84774188cbaSGuenter Roeck 	devres_add(dev, ptr);
84874188cbaSGuenter Roeck 	return hwdev;
84974188cbaSGuenter Roeck 
85074188cbaSGuenter Roeck error:
85174188cbaSGuenter Roeck 	devres_free(ptr);
85274188cbaSGuenter Roeck 	return hwdev;
85374188cbaSGuenter Roeck }
85474188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
85574188cbaSGuenter Roeck 
856d560168bSGuenter Roeck /**
857d560168bSGuenter Roeck  * devm_hwmon_device_register_with_info - register w/ hwmon
858d560168bSGuenter Roeck  * @dev:	the parent device
859d560168bSGuenter Roeck  * @name:	hwmon name attribute
860d560168bSGuenter Roeck  * @drvdata:	driver data to attach to created device
8613870945aSGuenter Roeck  * @chip:	pointer to hwmon chip information
8623870945aSGuenter Roeck  * @groups:	pointer to list of driver specific attribute groups
863d560168bSGuenter Roeck  *
864d560168bSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
865d560168bSGuenter Roeck  * unregistered with the parent device.
866d560168bSGuenter Roeck  */
867d560168bSGuenter Roeck struct device *
868d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name,
869d560168bSGuenter Roeck 				     void *drvdata,
870d560168bSGuenter Roeck 				     const struct hwmon_chip_info *chip,
871d560168bSGuenter Roeck 				     const struct attribute_group **groups)
872d560168bSGuenter Roeck {
873d560168bSGuenter Roeck 	struct device **ptr, *hwdev;
874d560168bSGuenter Roeck 
875d560168bSGuenter Roeck 	if (!dev)
876d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
877d560168bSGuenter Roeck 
878d560168bSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
879d560168bSGuenter Roeck 	if (!ptr)
880d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
881d560168bSGuenter Roeck 
882d560168bSGuenter Roeck 	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
883d560168bSGuenter Roeck 						groups);
884d560168bSGuenter Roeck 	if (IS_ERR(hwdev))
885d560168bSGuenter Roeck 		goto error;
886d560168bSGuenter Roeck 
887d560168bSGuenter Roeck 	*ptr = hwdev;
888d560168bSGuenter Roeck 	devres_add(dev, ptr);
889d560168bSGuenter Roeck 
890d560168bSGuenter Roeck 	return hwdev;
891d560168bSGuenter Roeck 
892d560168bSGuenter Roeck error:
893d560168bSGuenter Roeck 	devres_free(ptr);
894d560168bSGuenter Roeck 	return hwdev;
895d560168bSGuenter Roeck }
896d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
897d560168bSGuenter Roeck 
89874188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data)
89974188cbaSGuenter Roeck {
90074188cbaSGuenter Roeck 	struct device **hwdev = res;
90174188cbaSGuenter Roeck 
90274188cbaSGuenter Roeck 	return *hwdev == data;
90374188cbaSGuenter Roeck }
90474188cbaSGuenter Roeck 
90574188cbaSGuenter Roeck /**
90674188cbaSGuenter Roeck  * devm_hwmon_device_unregister - removes a previously registered hwmon device
90774188cbaSGuenter Roeck  *
90874188cbaSGuenter Roeck  * @dev: the parent device of the device to unregister
90974188cbaSGuenter Roeck  */
91074188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev)
91174188cbaSGuenter Roeck {
91274188cbaSGuenter Roeck 	WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
91374188cbaSGuenter Roeck }
91474188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
91574188cbaSGuenter Roeck 
9162958b1ecSJean Delvare static void __init hwmon_pci_quirks(void)
9172958b1ecSJean Delvare {
9182958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI
9192958b1ecSJean Delvare 	struct pci_dev *sb;
9202958b1ecSJean Delvare 	u16 base;
9212958b1ecSJean Delvare 	u8 enable;
9222958b1ecSJean Delvare 
9232958b1ecSJean Delvare 	/* Open access to 0x295-0x296 on MSI MS-7031 */
9242958b1ecSJean Delvare 	sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
925d6dab7ddSJean Delvare 	if (sb) {
926d6dab7ddSJean Delvare 		if (sb->subsystem_vendor == 0x1462 &&	/* MSI */
927d6dab7ddSJean Delvare 		    sb->subsystem_device == 0x0031) {	/* MS-7031 */
9282958b1ecSJean Delvare 			pci_read_config_byte(sb, 0x48, &enable);
9292958b1ecSJean Delvare 			pci_read_config_word(sb, 0x64, &base);
9302958b1ecSJean Delvare 
9312958b1ecSJean Delvare 			if (base == 0 && !(enable & BIT(2))) {
9322958b1ecSJean Delvare 				dev_info(&sb->dev,
9332958b1ecSJean Delvare 					 "Opening wide generic port at 0x295\n");
9342958b1ecSJean Delvare 				pci_write_config_word(sb, 0x64, 0x295);
935d6dab7ddSJean Delvare 				pci_write_config_byte(sb, 0x48,
936d6dab7ddSJean Delvare 						      enable | BIT(2));
9372958b1ecSJean Delvare 			}
9382958b1ecSJean Delvare 		}
939d6dab7ddSJean Delvare 		pci_dev_put(sb);
940d6dab7ddSJean Delvare 	}
9412958b1ecSJean Delvare #endif
9422958b1ecSJean Delvare }
9432958b1ecSJean Delvare 
9441236441fSMark M. Hoffman static int __init hwmon_init(void)
9451236441fSMark M. Hoffman {
946bab2243cSGuenter Roeck 	int err;
947bab2243cSGuenter Roeck 
9482958b1ecSJean Delvare 	hwmon_pci_quirks();
9492958b1ecSJean Delvare 
950bab2243cSGuenter Roeck 	err = class_register(&hwmon_class);
951bab2243cSGuenter Roeck 	if (err) {
952bab2243cSGuenter Roeck 		pr_err("couldn't register hwmon sysfs class\n");
953bab2243cSGuenter Roeck 		return err;
9541236441fSMark M. Hoffman 	}
9551236441fSMark M. Hoffman 	return 0;
9561236441fSMark M. Hoffman }
9571236441fSMark M. Hoffman 
9581236441fSMark M. Hoffman static void __exit hwmon_exit(void)
9591236441fSMark M. Hoffman {
960bab2243cSGuenter Roeck 	class_unregister(&hwmon_class);
9611236441fSMark M. Hoffman }
9621236441fSMark M. Hoffman 
96337f54ee5SDavid Brownell subsys_initcall(hwmon_init);
9641236441fSMark M. Hoffman module_exit(hwmon_exit);
9651236441fSMark M. Hoffman 
9661236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
9671236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
9681236441fSMark M. Hoffman MODULE_LICENSE("GPL");
9691236441fSMark M. Hoffman 
970