xref: /linux/drivers/hwmon/hwmon.c (revision b308f5c744522de020da4706718de9076adeada7)
11236441fSMark M. Hoffman /*
25ed04880SGuenter Roeck  * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
35ed04880SGuenter Roeck  *
45ed04880SGuenter Roeck  * This file defines the sysfs class "hwmon", for use by sensors drivers.
55ed04880SGuenter Roeck  *
65ed04880SGuenter Roeck  * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
75ed04880SGuenter Roeck  *
85ed04880SGuenter Roeck  * This program is free software; you can redistribute it and/or modify
95ed04880SGuenter Roeck  * it under the terms of the GNU General Public License as published by
105ed04880SGuenter Roeck  * the Free Software Foundation; version 2 of the License.
111236441fSMark M. Hoffman  */
121236441fSMark M. Hoffman 
13c95df1aeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14c95df1aeSJoe Perches 
15d560168bSGuenter Roeck #include <linux/bitops.h>
161236441fSMark M. Hoffman #include <linux/device.h>
171236441fSMark M. Hoffman #include <linux/err.h>
188c65b4a6STim Schmielau #include <linux/gfp.h>
19c9ebbe6fSGuenter Roeck #include <linux/hwmon.h>
20c9ebbe6fSGuenter Roeck #include <linux/idr.h>
21c9ebbe6fSGuenter Roeck #include <linux/module.h>
222958b1ecSJean Delvare #include <linux/pci.h>
23c9ebbe6fSGuenter Roeck #include <linux/slab.h>
24648cd48cSGuenter Roeck #include <linux/string.h>
25d560168bSGuenter Roeck #include <linux/thermal.h>
261236441fSMark M. Hoffman 
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 
41d560168bSGuenter Roeck struct hwmon_device_attribute {
42d560168bSGuenter Roeck 	struct device_attribute dev_attr;
43d560168bSGuenter Roeck 	const struct hwmon_ops *ops;
44d560168bSGuenter Roeck 	enum hwmon_sensor_types type;
45d560168bSGuenter Roeck 	u32 attr;
46d560168bSGuenter Roeck 	int index;
47d560168bSGuenter Roeck };
48d560168bSGuenter Roeck 
49d560168bSGuenter Roeck #define to_hwmon_attr(d) \
50d560168bSGuenter Roeck 	container_of(d, struct hwmon_device_attribute, dev_attr)
51d560168bSGuenter Roeck 
52d560168bSGuenter Roeck /*
53d560168bSGuenter Roeck  * Thermal zone information
54d560168bSGuenter Roeck  * In addition to the reference to the hwmon device,
55d560168bSGuenter Roeck  * also provides the sensor index.
56d560168bSGuenter Roeck  */
57d560168bSGuenter Roeck struct hwmon_thermal_data {
58d560168bSGuenter Roeck 	struct hwmon_device *hwdev;	/* Reference to hwmon device */
59d560168bSGuenter Roeck 	int index;			/* sensor index */
60d560168bSGuenter Roeck };
61d560168bSGuenter Roeck 
62bab2243cSGuenter Roeck static ssize_t
63bab2243cSGuenter Roeck show_name(struct device *dev, struct device_attribute *attr, char *buf)
64bab2243cSGuenter Roeck {
65bab2243cSGuenter Roeck 	return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
66bab2243cSGuenter Roeck }
67bab2243cSGuenter Roeck static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
68bab2243cSGuenter Roeck 
69bab2243cSGuenter Roeck static struct attribute *hwmon_dev_attrs[] = {
70bab2243cSGuenter Roeck 	&dev_attr_name.attr,
71bab2243cSGuenter Roeck 	NULL
72bab2243cSGuenter Roeck };
73bab2243cSGuenter Roeck 
74bab2243cSGuenter Roeck static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
75bab2243cSGuenter Roeck 					 struct attribute *attr, int n)
76bab2243cSGuenter Roeck {
77bab2243cSGuenter Roeck 	struct device *dev = container_of(kobj, struct device, kobj);
78bab2243cSGuenter Roeck 
79bab2243cSGuenter Roeck 	if (to_hwmon_device(dev)->name == NULL)
80bab2243cSGuenter Roeck 		return 0;
81bab2243cSGuenter Roeck 
82bab2243cSGuenter Roeck 	return attr->mode;
83bab2243cSGuenter Roeck }
84bab2243cSGuenter Roeck 
85bab2243cSGuenter Roeck static struct attribute_group hwmon_dev_attr_group = {
86bab2243cSGuenter Roeck 	.attrs		= hwmon_dev_attrs,
87bab2243cSGuenter Roeck 	.is_visible	= hwmon_dev_name_is_visible,
88bab2243cSGuenter Roeck };
89bab2243cSGuenter Roeck 
90bab2243cSGuenter Roeck static const struct attribute_group *hwmon_dev_attr_groups[] = {
91bab2243cSGuenter Roeck 	&hwmon_dev_attr_group,
92bab2243cSGuenter Roeck 	NULL
93bab2243cSGuenter Roeck };
94bab2243cSGuenter Roeck 
95bab2243cSGuenter Roeck static void hwmon_dev_release(struct device *dev)
96bab2243cSGuenter Roeck {
97bab2243cSGuenter Roeck 	kfree(to_hwmon_device(dev));
98bab2243cSGuenter Roeck }
99bab2243cSGuenter Roeck 
100bab2243cSGuenter Roeck static struct class hwmon_class = {
101bab2243cSGuenter Roeck 	.name = "hwmon",
102bab2243cSGuenter Roeck 	.owner = THIS_MODULE,
103bab2243cSGuenter Roeck 	.dev_groups = hwmon_dev_attr_groups,
104bab2243cSGuenter Roeck 	.dev_release = hwmon_dev_release,
105bab2243cSGuenter Roeck };
1061236441fSMark M. Hoffman 
1074ca5f468SJonathan Cameron static DEFINE_IDA(hwmon_ida);
1081236441fSMark M. Hoffman 
109d560168bSGuenter Roeck /* Thermal zone handling */
110d560168bSGuenter Roeck 
111d560168bSGuenter Roeck #if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF)
112d560168bSGuenter Roeck static int hwmon_thermal_get_temp(void *data, int *temp)
113d560168bSGuenter Roeck {
114d560168bSGuenter Roeck 	struct hwmon_thermal_data *tdata = data;
115d560168bSGuenter Roeck 	struct hwmon_device *hwdev = tdata->hwdev;
116d560168bSGuenter Roeck 	int ret;
117d560168bSGuenter Roeck 	long t;
118d560168bSGuenter Roeck 
119d560168bSGuenter Roeck 	ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
120d560168bSGuenter Roeck 				     tdata->index, &t);
121d560168bSGuenter Roeck 	if (ret < 0)
122d560168bSGuenter Roeck 		return ret;
123d560168bSGuenter Roeck 
124d560168bSGuenter Roeck 	*temp = t;
125d560168bSGuenter Roeck 
126d560168bSGuenter Roeck 	return 0;
127d560168bSGuenter Roeck }
128d560168bSGuenter Roeck 
129d560168bSGuenter Roeck static struct thermal_zone_of_device_ops hwmon_thermal_ops = {
130d560168bSGuenter Roeck 	.get_temp = hwmon_thermal_get_temp,
131d560168bSGuenter Roeck };
132d560168bSGuenter Roeck 
133d560168bSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev,
134d560168bSGuenter Roeck 				    struct hwmon_device *hwdev, int index)
135d560168bSGuenter Roeck {
136d560168bSGuenter Roeck 	struct hwmon_thermal_data *tdata;
137d560168bSGuenter Roeck 
138d560168bSGuenter Roeck 	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
139d560168bSGuenter Roeck 	if (!tdata)
140d560168bSGuenter Roeck 		return -ENOMEM;
141d560168bSGuenter Roeck 
142d560168bSGuenter Roeck 	tdata->hwdev = hwdev;
143d560168bSGuenter Roeck 	tdata->index = index;
144d560168bSGuenter Roeck 
145d560168bSGuenter Roeck 	devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
146d560168bSGuenter Roeck 					     &hwmon_thermal_ops);
147d560168bSGuenter Roeck 
148d560168bSGuenter Roeck 	return 0;
149d560168bSGuenter Roeck }
150d560168bSGuenter Roeck #else
151d560168bSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev,
152d560168bSGuenter Roeck 				    struct hwmon_device *hwdev, int index)
153d560168bSGuenter Roeck {
154d560168bSGuenter Roeck 	return 0;
155d560168bSGuenter Roeck }
156d560168bSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) */
157d560168bSGuenter Roeck 
158d560168bSGuenter Roeck /* sysfs attribute management */
159d560168bSGuenter Roeck 
160d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev,
161d560168bSGuenter Roeck 			       struct device_attribute *devattr, char *buf)
162d560168bSGuenter Roeck {
163d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
164d560168bSGuenter Roeck 	long val;
165d560168bSGuenter Roeck 	int ret;
166d560168bSGuenter Roeck 
167d560168bSGuenter Roeck 	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
168d560168bSGuenter Roeck 			       &val);
169d560168bSGuenter Roeck 	if (ret < 0)
170d560168bSGuenter Roeck 		return ret;
171d560168bSGuenter Roeck 
172d560168bSGuenter Roeck 	return sprintf(buf, "%ld\n", val);
173d560168bSGuenter Roeck }
174d560168bSGuenter Roeck 
175d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev,
176d560168bSGuenter Roeck 				struct device_attribute *devattr,
177d560168bSGuenter Roeck 				const char *buf, size_t count)
178d560168bSGuenter Roeck {
179d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
180d560168bSGuenter Roeck 	long val;
181d560168bSGuenter Roeck 	int ret;
182d560168bSGuenter Roeck 
183d560168bSGuenter Roeck 	ret = kstrtol(buf, 10, &val);
184d560168bSGuenter Roeck 	if (ret < 0)
185d560168bSGuenter Roeck 		return ret;
186d560168bSGuenter Roeck 
187d560168bSGuenter Roeck 	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
188d560168bSGuenter Roeck 				val);
189d560168bSGuenter Roeck 	if (ret < 0)
190d560168bSGuenter Roeck 		return ret;
191d560168bSGuenter Roeck 
192d560168bSGuenter Roeck 	return count;
193d560168bSGuenter Roeck }
194d560168bSGuenter Roeck 
195d560168bSGuenter Roeck static int hwmon_attr_base(enum hwmon_sensor_types type)
196d560168bSGuenter Roeck {
197d560168bSGuenter Roeck 	if (type == hwmon_in)
198d560168bSGuenter Roeck 		return 0;
199d560168bSGuenter Roeck 	return 1;
200d560168bSGuenter Roeck }
201d560168bSGuenter Roeck 
202d560168bSGuenter Roeck static struct attribute *hwmon_genattr(struct device *dev,
203d560168bSGuenter Roeck 				       const void *drvdata,
204d560168bSGuenter Roeck 				       enum hwmon_sensor_types type,
205d560168bSGuenter Roeck 				       u32 attr,
206d560168bSGuenter Roeck 				       int index,
207d560168bSGuenter Roeck 				       const char *template,
208d560168bSGuenter Roeck 				       const struct hwmon_ops *ops)
209d560168bSGuenter Roeck {
210d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr;
211d560168bSGuenter Roeck 	struct device_attribute *dattr;
212d560168bSGuenter Roeck 	struct attribute *a;
213d560168bSGuenter Roeck 	umode_t mode;
214d560168bSGuenter Roeck 	char *name;
215d560168bSGuenter Roeck 
216d560168bSGuenter Roeck 	/* The attribute is invisible if there is no template string */
217d560168bSGuenter Roeck 	if (!template)
218d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
219d560168bSGuenter Roeck 
220d560168bSGuenter Roeck 	mode = ops->is_visible(drvdata, type, attr, index);
221d560168bSGuenter Roeck 	if (!mode)
222d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
223d560168bSGuenter Roeck 
224d560168bSGuenter Roeck 	if ((mode & S_IRUGO) && !ops->read)
225d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
226d560168bSGuenter Roeck 	if ((mode & S_IWUGO) && !ops->write)
227d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
228d560168bSGuenter Roeck 
229d560168bSGuenter Roeck 	if (type == hwmon_chip) {
230d560168bSGuenter Roeck 		name = (char *)template;
231d560168bSGuenter Roeck 	} else {
232d560168bSGuenter Roeck 		name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL);
233d560168bSGuenter Roeck 		if (!name)
234d560168bSGuenter Roeck 			return ERR_PTR(-ENOMEM);
235d560168bSGuenter Roeck 		scnprintf(name, strlen(template) + 16, template,
236d560168bSGuenter Roeck 			  index + hwmon_attr_base(type));
237d560168bSGuenter Roeck 	}
238d560168bSGuenter Roeck 
239d560168bSGuenter Roeck 	hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
240d560168bSGuenter Roeck 	if (!hattr)
241d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
242d560168bSGuenter Roeck 
243d560168bSGuenter Roeck 	hattr->type = type;
244d560168bSGuenter Roeck 	hattr->attr = attr;
245d560168bSGuenter Roeck 	hattr->index = index;
246d560168bSGuenter Roeck 	hattr->ops = ops;
247d560168bSGuenter Roeck 
248d560168bSGuenter Roeck 	dattr = &hattr->dev_attr;
249d560168bSGuenter Roeck 	dattr->show = hwmon_attr_show;
250d560168bSGuenter Roeck 	dattr->store = hwmon_attr_store;
251d560168bSGuenter Roeck 
252d560168bSGuenter Roeck 	a = &dattr->attr;
253d560168bSGuenter Roeck 	sysfs_attr_init(a);
254d560168bSGuenter Roeck 	a->name = name;
255d560168bSGuenter Roeck 	a->mode = mode;
256d560168bSGuenter Roeck 
257d560168bSGuenter Roeck 	return a;
258d560168bSGuenter Roeck }
259d560168bSGuenter Roeck 
260d560168bSGuenter Roeck static const char * const hwmon_chip_attr_templates[] = {
261d560168bSGuenter Roeck 	[hwmon_chip_temp_reset_history] = "temp_reset_history",
26200d616cfSGuenter Roeck 	[hwmon_chip_in_reset_history] = "in_reset_history",
2639b26947cSGuenter Roeck 	[hwmon_chip_curr_reset_history] = "curr_reset_history",
264*b308f5c7SGuenter Roeck 	[hwmon_chip_power_reset_history] = "power_reset_history",
265d560168bSGuenter Roeck 	[hwmon_chip_update_interval] = "update_interval",
266d560168bSGuenter Roeck 	[hwmon_chip_alarms] = "alarms",
267d560168bSGuenter Roeck };
268d560168bSGuenter Roeck 
269d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = {
270d560168bSGuenter Roeck 	[hwmon_temp_input] = "temp%d_input",
271d560168bSGuenter Roeck 	[hwmon_temp_type] = "temp%d_type",
272d560168bSGuenter Roeck 	[hwmon_temp_lcrit] = "temp%d_lcrit",
273d560168bSGuenter Roeck 	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
274d560168bSGuenter Roeck 	[hwmon_temp_min] = "temp%d_min",
275d560168bSGuenter Roeck 	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
276d560168bSGuenter Roeck 	[hwmon_temp_max] = "temp%d_max",
277d560168bSGuenter Roeck 	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
278d560168bSGuenter Roeck 	[hwmon_temp_crit] = "temp%d_crit",
279d560168bSGuenter Roeck 	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
280d560168bSGuenter Roeck 	[hwmon_temp_emergency] = "temp%d_emergency",
281d560168bSGuenter Roeck 	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
282d560168bSGuenter Roeck 	[hwmon_temp_alarm] = "temp%d_alarm",
283d560168bSGuenter Roeck 	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
284d560168bSGuenter Roeck 	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
285d560168bSGuenter Roeck 	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
286d560168bSGuenter Roeck 	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
287d560168bSGuenter Roeck 	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
288d560168bSGuenter Roeck 	[hwmon_temp_fault] = "temp%d_fault",
289d560168bSGuenter Roeck 	[hwmon_temp_offset] = "temp%d_offset",
290d560168bSGuenter Roeck 	[hwmon_temp_label] = "temp%d_label",
291d560168bSGuenter Roeck 	[hwmon_temp_lowest] = "temp%d_lowest",
292d560168bSGuenter Roeck 	[hwmon_temp_highest] = "temp%d_highest",
293d560168bSGuenter Roeck 	[hwmon_temp_reset_history] = "temp%d_reset_history",
294d560168bSGuenter Roeck };
295d560168bSGuenter Roeck 
29600d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = {
29700d616cfSGuenter Roeck 	[hwmon_in_input] = "in%d_input",
29800d616cfSGuenter Roeck 	[hwmon_in_min] = "in%d_min",
29900d616cfSGuenter Roeck 	[hwmon_in_max] = "in%d_max",
30000d616cfSGuenter Roeck 	[hwmon_in_lcrit] = "in%d_lcrit",
30100d616cfSGuenter Roeck 	[hwmon_in_crit] = "in%d_crit",
30200d616cfSGuenter Roeck 	[hwmon_in_average] = "in%d_average",
30300d616cfSGuenter Roeck 	[hwmon_in_lowest] = "in%d_lowest",
30400d616cfSGuenter Roeck 	[hwmon_in_highest] = "in%d_highest",
30500d616cfSGuenter Roeck 	[hwmon_in_reset_history] = "in%d_reset_history",
30600d616cfSGuenter Roeck 	[hwmon_in_label] = "in%d_label",
30700d616cfSGuenter Roeck 	[hwmon_in_alarm] = "in%d_alarm",
30800d616cfSGuenter Roeck 	[hwmon_in_min_alarm] = "in%d_min_alarm",
30900d616cfSGuenter Roeck 	[hwmon_in_max_alarm] = "in%d_max_alarm",
31000d616cfSGuenter Roeck 	[hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
31100d616cfSGuenter Roeck 	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
31200d616cfSGuenter Roeck };
31300d616cfSGuenter Roeck 
3149b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = {
3159b26947cSGuenter Roeck 	[hwmon_curr_input] = "curr%d_input",
3169b26947cSGuenter Roeck 	[hwmon_curr_min] = "curr%d_min",
3179b26947cSGuenter Roeck 	[hwmon_curr_max] = "curr%d_max",
3189b26947cSGuenter Roeck 	[hwmon_curr_lcrit] = "curr%d_lcrit",
3199b26947cSGuenter Roeck 	[hwmon_curr_crit] = "curr%d_crit",
3209b26947cSGuenter Roeck 	[hwmon_curr_average] = "curr%d_average",
3219b26947cSGuenter Roeck 	[hwmon_curr_lowest] = "curr%d_lowest",
3229b26947cSGuenter Roeck 	[hwmon_curr_highest] = "curr%d_highest",
3239b26947cSGuenter Roeck 	[hwmon_curr_reset_history] = "curr%d_reset_history",
3249b26947cSGuenter Roeck 	[hwmon_curr_label] = "curr%d_label",
3259b26947cSGuenter Roeck 	[hwmon_curr_alarm] = "curr%d_alarm",
3269b26947cSGuenter Roeck 	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
3279b26947cSGuenter Roeck 	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
3289b26947cSGuenter Roeck 	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
3299b26947cSGuenter Roeck 	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
3309b26947cSGuenter Roeck };
3319b26947cSGuenter Roeck 
332*b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = {
333*b308f5c7SGuenter Roeck 	[hwmon_power_average] = "power%d_average",
334*b308f5c7SGuenter Roeck 	[hwmon_power_average_interval] = "power%d_average_interval",
335*b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_max] = "power%d_interval_max",
336*b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_min] = "power%d_interval_min",
337*b308f5c7SGuenter Roeck 	[hwmon_power_average_highest] = "power%d_average_highest",
338*b308f5c7SGuenter Roeck 	[hwmon_power_average_lowest] = "power%d_average_lowest",
339*b308f5c7SGuenter Roeck 	[hwmon_power_average_max] = "power%d_average_max",
340*b308f5c7SGuenter Roeck 	[hwmon_power_average_min] = "power%d_average_min",
341*b308f5c7SGuenter Roeck 	[hwmon_power_input] = "power%d_input",
342*b308f5c7SGuenter Roeck 	[hwmon_power_input_highest] = "power%d_input_highest",
343*b308f5c7SGuenter Roeck 	[hwmon_power_input_lowest] = "power%d_input_lowest",
344*b308f5c7SGuenter Roeck 	[hwmon_power_reset_history] = "power%d_reset_history",
345*b308f5c7SGuenter Roeck 	[hwmon_power_accuracy] = "power%d_accuracy",
346*b308f5c7SGuenter Roeck 	[hwmon_power_cap] = "power%d_cap",
347*b308f5c7SGuenter Roeck 	[hwmon_power_cap_hyst] = "power%d_cap_hyst",
348*b308f5c7SGuenter Roeck 	[hwmon_power_cap_max] = "power%d_cap_max",
349*b308f5c7SGuenter Roeck 	[hwmon_power_cap_min] = "power%d_cap_min",
350*b308f5c7SGuenter Roeck 	[hwmon_power_max] = "power%d_max",
351*b308f5c7SGuenter Roeck 	[hwmon_power_crit] = "power%d_crit",
352*b308f5c7SGuenter Roeck 	[hwmon_power_label] = "power%d_label",
353*b308f5c7SGuenter Roeck 	[hwmon_power_alarm] = "power%d_alarm",
354*b308f5c7SGuenter Roeck 	[hwmon_power_cap_alarm] = "power%d_cap_alarm",
355*b308f5c7SGuenter Roeck 	[hwmon_power_max_alarm] = "power%d_max_alarm",
356*b308f5c7SGuenter Roeck 	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
357*b308f5c7SGuenter Roeck };
358*b308f5c7SGuenter Roeck 
359d560168bSGuenter Roeck static const char * const *__templates[] = {
360d560168bSGuenter Roeck 	[hwmon_chip] = hwmon_chip_attr_templates,
361d560168bSGuenter Roeck 	[hwmon_temp] = hwmon_temp_attr_templates,
36200d616cfSGuenter Roeck 	[hwmon_in] = hwmon_in_attr_templates,
3639b26947cSGuenter Roeck 	[hwmon_curr] = hwmon_curr_attr_templates,
364*b308f5c7SGuenter Roeck 	[hwmon_power] = hwmon_power_attr_templates,
365d560168bSGuenter Roeck };
366d560168bSGuenter Roeck 
367d560168bSGuenter Roeck static const int __templates_size[] = {
368d560168bSGuenter Roeck 	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates),
369d560168bSGuenter Roeck 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
37000d616cfSGuenter Roeck 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
3719b26947cSGuenter Roeck 	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
372*b308f5c7SGuenter Roeck 	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
373d560168bSGuenter Roeck };
374d560168bSGuenter Roeck 
375d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
376d560168bSGuenter Roeck {
377d560168bSGuenter Roeck 	int i, n;
378d560168bSGuenter Roeck 
379d560168bSGuenter Roeck 	for (i = n = 0; info->config[i]; i++)
380d560168bSGuenter Roeck 		n += hweight32(info->config[i]);
381d560168bSGuenter Roeck 
382d560168bSGuenter Roeck 	return n;
383d560168bSGuenter Roeck }
384d560168bSGuenter Roeck 
385d560168bSGuenter Roeck static int hwmon_genattrs(struct device *dev,
386d560168bSGuenter Roeck 			  const void *drvdata,
387d560168bSGuenter Roeck 			  struct attribute **attrs,
388d560168bSGuenter Roeck 			  const struct hwmon_ops *ops,
389d560168bSGuenter Roeck 			  const struct hwmon_channel_info *info)
390d560168bSGuenter Roeck {
391d560168bSGuenter Roeck 	const char * const *templates;
392d560168bSGuenter Roeck 	int template_size;
393d560168bSGuenter Roeck 	int i, aindex = 0;
394d560168bSGuenter Roeck 
395d560168bSGuenter Roeck 	if (info->type >= ARRAY_SIZE(__templates))
396d560168bSGuenter Roeck 		return -EINVAL;
397d560168bSGuenter Roeck 
398d560168bSGuenter Roeck 	templates = __templates[info->type];
399d560168bSGuenter Roeck 	template_size = __templates_size[info->type];
400d560168bSGuenter Roeck 
401d560168bSGuenter Roeck 	for (i = 0; info->config[i]; i++) {
402d560168bSGuenter Roeck 		u32 attr_mask = info->config[i];
403d560168bSGuenter Roeck 		u32 attr;
404d560168bSGuenter Roeck 
405d560168bSGuenter Roeck 		while (attr_mask) {
406d560168bSGuenter Roeck 			struct attribute *a;
407d560168bSGuenter Roeck 
408d560168bSGuenter Roeck 			attr = __ffs(attr_mask);
409d560168bSGuenter Roeck 			attr_mask &= ~BIT(attr);
410d560168bSGuenter Roeck 			if (attr >= template_size)
411d560168bSGuenter Roeck 				return -EINVAL;
412d560168bSGuenter Roeck 			a = hwmon_genattr(dev, drvdata, info->type, attr, i,
413d560168bSGuenter Roeck 					  templates[attr], ops);
414d560168bSGuenter Roeck 			if (IS_ERR(a)) {
415d560168bSGuenter Roeck 				if (PTR_ERR(a) != -ENOENT)
416d560168bSGuenter Roeck 					return PTR_ERR(a);
417d560168bSGuenter Roeck 				continue;
418d560168bSGuenter Roeck 			}
419d560168bSGuenter Roeck 			attrs[aindex++] = a;
420d560168bSGuenter Roeck 		}
421d560168bSGuenter Roeck 	}
422d560168bSGuenter Roeck 	return aindex;
423d560168bSGuenter Roeck }
424d560168bSGuenter Roeck 
425d560168bSGuenter Roeck static struct attribute **
426d560168bSGuenter Roeck __hwmon_create_attrs(struct device *dev, const void *drvdata,
427d560168bSGuenter Roeck 		     const struct hwmon_chip_info *chip)
428d560168bSGuenter Roeck {
429d560168bSGuenter Roeck 	int ret, i, aindex = 0, nattrs = 0;
430d560168bSGuenter Roeck 	struct attribute **attrs;
431d560168bSGuenter Roeck 
432d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++)
433d560168bSGuenter Roeck 		nattrs += hwmon_num_channel_attrs(chip->info[i]);
434d560168bSGuenter Roeck 
435d560168bSGuenter Roeck 	if (nattrs == 0)
436d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
437d560168bSGuenter Roeck 
438d560168bSGuenter Roeck 	attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
439d560168bSGuenter Roeck 	if (!attrs)
440d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
441d560168bSGuenter Roeck 
442d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++) {
443d560168bSGuenter Roeck 		ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
444d560168bSGuenter Roeck 				     chip->info[i]);
445d560168bSGuenter Roeck 		if (ret < 0)
446d560168bSGuenter Roeck 			return ERR_PTR(ret);
447d560168bSGuenter Roeck 		aindex += ret;
448d560168bSGuenter Roeck 	}
449d560168bSGuenter Roeck 
450d560168bSGuenter Roeck 	return attrs;
451d560168bSGuenter Roeck }
452d560168bSGuenter Roeck 
453d560168bSGuenter Roeck static struct device *
454d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
455d560168bSGuenter Roeck 			const struct hwmon_chip_info *chip,
456d560168bSGuenter Roeck 			const struct attribute_group **groups)
457d560168bSGuenter Roeck {
458d560168bSGuenter Roeck 	struct hwmon_device *hwdev;
459d560168bSGuenter Roeck 	struct device *hdev;
460d560168bSGuenter Roeck 	int i, j, err, id;
461d560168bSGuenter Roeck 
462d560168bSGuenter Roeck 	/* Do not accept invalid characters in hwmon name attribute */
463d560168bSGuenter Roeck 	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
464d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
465d560168bSGuenter Roeck 
466d560168bSGuenter Roeck 	id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
467d560168bSGuenter Roeck 	if (id < 0)
468d560168bSGuenter Roeck 		return ERR_PTR(id);
469d560168bSGuenter Roeck 
470d560168bSGuenter Roeck 	hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
471d560168bSGuenter Roeck 	if (hwdev == NULL) {
472d560168bSGuenter Roeck 		err = -ENOMEM;
473d560168bSGuenter Roeck 		goto ida_remove;
474d560168bSGuenter Roeck 	}
475d560168bSGuenter Roeck 
476d560168bSGuenter Roeck 	hdev = &hwdev->dev;
477d560168bSGuenter Roeck 
478d560168bSGuenter Roeck 	if (chip && chip->ops->is_visible) {
479d560168bSGuenter Roeck 		struct attribute **attrs;
480d560168bSGuenter Roeck 		int ngroups = 2;
481d560168bSGuenter Roeck 
482d560168bSGuenter Roeck 		if (groups)
483d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
484d560168bSGuenter Roeck 				ngroups++;
485d560168bSGuenter Roeck 
486d560168bSGuenter Roeck 		hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
487d560168bSGuenter Roeck 					     GFP_KERNEL);
488d560168bSGuenter Roeck 		if (!hwdev->groups)
489d560168bSGuenter Roeck 			return ERR_PTR(-ENOMEM);
490d560168bSGuenter Roeck 
491d560168bSGuenter Roeck 		attrs = __hwmon_create_attrs(dev, drvdata, chip);
492d560168bSGuenter Roeck 		if (IS_ERR(attrs)) {
493d560168bSGuenter Roeck 			err = PTR_ERR(attrs);
494d560168bSGuenter Roeck 			goto free_hwmon;
495d560168bSGuenter Roeck 		}
496d560168bSGuenter Roeck 
497d560168bSGuenter Roeck 		hwdev->group.attrs = attrs;
498d560168bSGuenter Roeck 		ngroups = 0;
499d560168bSGuenter Roeck 		hwdev->groups[ngroups++] = &hwdev->group;
500d560168bSGuenter Roeck 
501d560168bSGuenter Roeck 		if (groups) {
502d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
503d560168bSGuenter Roeck 				hwdev->groups[ngroups++] = groups[i];
504d560168bSGuenter Roeck 		}
505d560168bSGuenter Roeck 
506d560168bSGuenter Roeck 		hdev->groups = hwdev->groups;
507d560168bSGuenter Roeck 	} else {
508d560168bSGuenter Roeck 		hdev->groups = groups;
509d560168bSGuenter Roeck 	}
510d560168bSGuenter Roeck 
511d560168bSGuenter Roeck 	hwdev->name = name;
512d560168bSGuenter Roeck 	hdev->class = &hwmon_class;
513d560168bSGuenter Roeck 	hdev->parent = dev;
514d560168bSGuenter Roeck 	hdev->of_node = dev ? dev->of_node : NULL;
515d560168bSGuenter Roeck 	hwdev->chip = chip;
516d560168bSGuenter Roeck 	dev_set_drvdata(hdev, drvdata);
517d560168bSGuenter Roeck 	dev_set_name(hdev, HWMON_ID_FORMAT, id);
518d560168bSGuenter Roeck 	err = device_register(hdev);
519d560168bSGuenter Roeck 	if (err)
520d560168bSGuenter Roeck 		goto free_hwmon;
521d560168bSGuenter Roeck 
522d560168bSGuenter Roeck 	if (chip && chip->ops->is_visible && chip->ops->read &&
523d560168bSGuenter Roeck 	    chip->info[0]->type == hwmon_chip &&
524d560168bSGuenter Roeck 	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
525d560168bSGuenter Roeck 		const struct hwmon_channel_info **info = chip->info;
526d560168bSGuenter Roeck 
527d560168bSGuenter Roeck 		for (i = 1; info[i]; i++) {
528d560168bSGuenter Roeck 			if (info[i]->type != hwmon_temp)
529d560168bSGuenter Roeck 				continue;
530d560168bSGuenter Roeck 
531d560168bSGuenter Roeck 			for (j = 0; info[i]->config[j]; j++) {
532d560168bSGuenter Roeck 				if (!chip->ops->is_visible(drvdata, hwmon_temp,
533d560168bSGuenter Roeck 							   hwmon_temp_input, j))
534d560168bSGuenter Roeck 					continue;
535d560168bSGuenter Roeck 				if (info[i]->config[j] & HWMON_T_INPUT)
536d560168bSGuenter Roeck 					hwmon_thermal_add_sensor(dev, hwdev, j);
537d560168bSGuenter Roeck 			}
538d560168bSGuenter Roeck 		}
539d560168bSGuenter Roeck 	}
540d560168bSGuenter Roeck 
541d560168bSGuenter Roeck 	return hdev;
542d560168bSGuenter Roeck 
543d560168bSGuenter Roeck free_hwmon:
544d560168bSGuenter Roeck 	kfree(hwdev);
545d560168bSGuenter Roeck ida_remove:
546d560168bSGuenter Roeck 	ida_simple_remove(&hwmon_ida, id);
547d560168bSGuenter Roeck 	return ERR_PTR(err);
548d560168bSGuenter Roeck }
549d560168bSGuenter Roeck 
5501236441fSMark M. Hoffman /**
551bab2243cSGuenter Roeck  * hwmon_device_register_with_groups - register w/ hwmon
552bab2243cSGuenter Roeck  * @dev: the parent device
553bab2243cSGuenter Roeck  * @name: hwmon name attribute
554bab2243cSGuenter Roeck  * @drvdata: driver data to attach to created device
555bab2243cSGuenter Roeck  * @groups: List of attribute groups to create
556bab2243cSGuenter Roeck  *
557bab2243cSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
558bab2243cSGuenter Roeck  * longer needed.
559bab2243cSGuenter Roeck  *
560bab2243cSGuenter Roeck  * Returns the pointer to the new device.
561bab2243cSGuenter Roeck  */
562bab2243cSGuenter Roeck struct device *
563bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name,
564bab2243cSGuenter Roeck 				  void *drvdata,
565bab2243cSGuenter Roeck 				  const struct attribute_group **groups)
566bab2243cSGuenter Roeck {
567d560168bSGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
568bab2243cSGuenter Roeck }
569bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
570bab2243cSGuenter Roeck 
571bab2243cSGuenter Roeck /**
572d560168bSGuenter Roeck  * hwmon_device_register_with_info - register w/ hwmon
573d560168bSGuenter Roeck  * @dev: the parent device
574d560168bSGuenter Roeck  * @name: hwmon name attribute
575d560168bSGuenter Roeck  * @drvdata: driver data to attach to created device
576d560168bSGuenter Roeck  * @info: Pointer to hwmon chip information
577d560168bSGuenter Roeck  * @groups - pointer to list of driver specific attribute groups
578d560168bSGuenter Roeck  *
579d560168bSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
580d560168bSGuenter Roeck  * longer needed.
581d560168bSGuenter Roeck  *
582d560168bSGuenter Roeck  * Returns the pointer to the new device.
583d560168bSGuenter Roeck  */
584d560168bSGuenter Roeck struct device *
585d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name,
586d560168bSGuenter Roeck 				void *drvdata,
587d560168bSGuenter Roeck 				const struct hwmon_chip_info *chip,
588d560168bSGuenter Roeck 				const struct attribute_group **groups)
589d560168bSGuenter Roeck {
590d560168bSGuenter Roeck 	if (chip && (!chip->ops || !chip->info))
591d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
592d560168bSGuenter Roeck 
593d560168bSGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, chip, groups);
594d560168bSGuenter Roeck }
595d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
596d560168bSGuenter Roeck 
597d560168bSGuenter Roeck /**
5981beeffe4STony Jones  * hwmon_device_register - register w/ hwmon
5991236441fSMark M. Hoffman  * @dev: the device to register
6001236441fSMark M. Hoffman  *
6011beeffe4STony Jones  * hwmon_device_unregister() must be called when the device is no
6021236441fSMark M. Hoffman  * longer needed.
6031236441fSMark M. Hoffman  *
6041beeffe4STony Jones  * Returns the pointer to the new device.
6051236441fSMark M. Hoffman  */
6061beeffe4STony Jones struct device *hwmon_device_register(struct device *dev)
6071236441fSMark M. Hoffman {
608bab2243cSGuenter Roeck 	return hwmon_device_register_with_groups(dev, NULL, NULL, NULL);
6091236441fSMark M. Hoffman }
610839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register);
6111236441fSMark M. Hoffman 
6121236441fSMark M. Hoffman /**
6131236441fSMark M. Hoffman  * hwmon_device_unregister - removes the previously registered class device
6141236441fSMark M. Hoffman  *
6151beeffe4STony Jones  * @dev: the class device to destroy
6161236441fSMark M. Hoffman  */
6171beeffe4STony Jones void hwmon_device_unregister(struct device *dev)
6181236441fSMark M. Hoffman {
6191236441fSMark M. Hoffman 	int id;
6201236441fSMark M. Hoffman 
621739cf3a2SKay Sievers 	if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
6221beeffe4STony Jones 		device_unregister(dev);
6234ca5f468SJonathan Cameron 		ida_simple_remove(&hwmon_ida, id);
6241236441fSMark M. Hoffman 	} else
6251beeffe4STony Jones 		dev_dbg(dev->parent,
6261236441fSMark M. Hoffman 			"hwmon_device_unregister() failed: bad class ID!\n");
6271236441fSMark M. Hoffman }
628839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister);
6291236441fSMark M. Hoffman 
63074188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res)
63174188cbaSGuenter Roeck {
63274188cbaSGuenter Roeck 	struct device *hwdev = *(struct device **)res;
63374188cbaSGuenter Roeck 
63474188cbaSGuenter Roeck 	hwmon_device_unregister(hwdev);
63574188cbaSGuenter Roeck }
63674188cbaSGuenter Roeck 
63774188cbaSGuenter Roeck /**
63874188cbaSGuenter Roeck  * devm_hwmon_device_register_with_groups - register w/ hwmon
63974188cbaSGuenter Roeck  * @dev: the parent device
64074188cbaSGuenter Roeck  * @name: hwmon name attribute
64174188cbaSGuenter Roeck  * @drvdata: driver data to attach to created device
64274188cbaSGuenter Roeck  * @groups: List of attribute groups to create
64374188cbaSGuenter Roeck  *
64474188cbaSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
64574188cbaSGuenter Roeck  * unregistered with the parent device.
64674188cbaSGuenter Roeck  */
64774188cbaSGuenter Roeck struct device *
64874188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
64974188cbaSGuenter Roeck 				       void *drvdata,
65074188cbaSGuenter Roeck 				       const struct attribute_group **groups)
65174188cbaSGuenter Roeck {
65274188cbaSGuenter Roeck 	struct device **ptr, *hwdev;
65374188cbaSGuenter Roeck 
65474188cbaSGuenter Roeck 	if (!dev)
65574188cbaSGuenter Roeck 		return ERR_PTR(-EINVAL);
65674188cbaSGuenter Roeck 
65774188cbaSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
65874188cbaSGuenter Roeck 	if (!ptr)
65974188cbaSGuenter Roeck 		return ERR_PTR(-ENOMEM);
66074188cbaSGuenter Roeck 
66174188cbaSGuenter Roeck 	hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
66274188cbaSGuenter Roeck 	if (IS_ERR(hwdev))
66374188cbaSGuenter Roeck 		goto error;
66474188cbaSGuenter Roeck 
66574188cbaSGuenter Roeck 	*ptr = hwdev;
66674188cbaSGuenter Roeck 	devres_add(dev, ptr);
66774188cbaSGuenter Roeck 	return hwdev;
66874188cbaSGuenter Roeck 
66974188cbaSGuenter Roeck error:
67074188cbaSGuenter Roeck 	devres_free(ptr);
67174188cbaSGuenter Roeck 	return hwdev;
67274188cbaSGuenter Roeck }
67374188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
67474188cbaSGuenter Roeck 
675d560168bSGuenter Roeck /**
676d560168bSGuenter Roeck  * devm_hwmon_device_register_with_info - register w/ hwmon
677d560168bSGuenter Roeck  * @dev: the parent device
678d560168bSGuenter Roeck  * @name: hwmon name attribute
679d560168bSGuenter Roeck  * @drvdata: driver data to attach to created device
680d560168bSGuenter Roeck  * @info: Pointer to hwmon chip information
681d560168bSGuenter Roeck  * @groups - pointer to list of driver specific attribute groups
682d560168bSGuenter Roeck  *
683d560168bSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
684d560168bSGuenter Roeck  * unregistered with the parent device.
685d560168bSGuenter Roeck  */
686d560168bSGuenter Roeck struct device *
687d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name,
688d560168bSGuenter Roeck 				     void *drvdata,
689d560168bSGuenter Roeck 				     const struct hwmon_chip_info *chip,
690d560168bSGuenter Roeck 				     const struct attribute_group **groups)
691d560168bSGuenter Roeck {
692d560168bSGuenter Roeck 	struct device **ptr, *hwdev;
693d560168bSGuenter Roeck 
694d560168bSGuenter Roeck 	if (!dev)
695d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
696d560168bSGuenter Roeck 
697d560168bSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
698d560168bSGuenter Roeck 	if (!ptr)
699d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
700d560168bSGuenter Roeck 
701d560168bSGuenter Roeck 	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
702d560168bSGuenter Roeck 						groups);
703d560168bSGuenter Roeck 	if (IS_ERR(hwdev))
704d560168bSGuenter Roeck 		goto error;
705d560168bSGuenter Roeck 
706d560168bSGuenter Roeck 	*ptr = hwdev;
707d560168bSGuenter Roeck 	devres_add(dev, ptr);
708d560168bSGuenter Roeck 
709d560168bSGuenter Roeck 	return hwdev;
710d560168bSGuenter Roeck 
711d560168bSGuenter Roeck error:
712d560168bSGuenter Roeck 	devres_free(ptr);
713d560168bSGuenter Roeck 	return hwdev;
714d560168bSGuenter Roeck }
715d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
716d560168bSGuenter Roeck 
71774188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data)
71874188cbaSGuenter Roeck {
71974188cbaSGuenter Roeck 	struct device **hwdev = res;
72074188cbaSGuenter Roeck 
72174188cbaSGuenter Roeck 	return *hwdev == data;
72274188cbaSGuenter Roeck }
72374188cbaSGuenter Roeck 
72474188cbaSGuenter Roeck /**
72574188cbaSGuenter Roeck  * devm_hwmon_device_unregister - removes a previously registered hwmon device
72674188cbaSGuenter Roeck  *
72774188cbaSGuenter Roeck  * @dev: the parent device of the device to unregister
72874188cbaSGuenter Roeck  */
72974188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev)
73074188cbaSGuenter Roeck {
73174188cbaSGuenter Roeck 	WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
73274188cbaSGuenter Roeck }
73374188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
73474188cbaSGuenter Roeck 
7352958b1ecSJean Delvare static void __init hwmon_pci_quirks(void)
7362958b1ecSJean Delvare {
7372958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI
7382958b1ecSJean Delvare 	struct pci_dev *sb;
7392958b1ecSJean Delvare 	u16 base;
7402958b1ecSJean Delvare 	u8 enable;
7412958b1ecSJean Delvare 
7422958b1ecSJean Delvare 	/* Open access to 0x295-0x296 on MSI MS-7031 */
7432958b1ecSJean Delvare 	sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
744d6dab7ddSJean Delvare 	if (sb) {
745d6dab7ddSJean Delvare 		if (sb->subsystem_vendor == 0x1462 &&	/* MSI */
746d6dab7ddSJean Delvare 		    sb->subsystem_device == 0x0031) {	/* MS-7031 */
7472958b1ecSJean Delvare 			pci_read_config_byte(sb, 0x48, &enable);
7482958b1ecSJean Delvare 			pci_read_config_word(sb, 0x64, &base);
7492958b1ecSJean Delvare 
7502958b1ecSJean Delvare 			if (base == 0 && !(enable & BIT(2))) {
7512958b1ecSJean Delvare 				dev_info(&sb->dev,
7522958b1ecSJean Delvare 					 "Opening wide generic port at 0x295\n");
7532958b1ecSJean Delvare 				pci_write_config_word(sb, 0x64, 0x295);
754d6dab7ddSJean Delvare 				pci_write_config_byte(sb, 0x48,
755d6dab7ddSJean Delvare 						      enable | BIT(2));
7562958b1ecSJean Delvare 			}
7572958b1ecSJean Delvare 		}
758d6dab7ddSJean Delvare 		pci_dev_put(sb);
759d6dab7ddSJean Delvare 	}
7602958b1ecSJean Delvare #endif
7612958b1ecSJean Delvare }
7622958b1ecSJean Delvare 
7631236441fSMark M. Hoffman static int __init hwmon_init(void)
7641236441fSMark M. Hoffman {
765bab2243cSGuenter Roeck 	int err;
766bab2243cSGuenter Roeck 
7672958b1ecSJean Delvare 	hwmon_pci_quirks();
7682958b1ecSJean Delvare 
769bab2243cSGuenter Roeck 	err = class_register(&hwmon_class);
770bab2243cSGuenter Roeck 	if (err) {
771bab2243cSGuenter Roeck 		pr_err("couldn't register hwmon sysfs class\n");
772bab2243cSGuenter Roeck 		return err;
7731236441fSMark M. Hoffman 	}
7741236441fSMark M. Hoffman 	return 0;
7751236441fSMark M. Hoffman }
7761236441fSMark M. Hoffman 
7771236441fSMark M. Hoffman static void __exit hwmon_exit(void)
7781236441fSMark M. Hoffman {
779bab2243cSGuenter Roeck 	class_unregister(&hwmon_class);
7801236441fSMark M. Hoffman }
7811236441fSMark M. Hoffman 
78237f54ee5SDavid Brownell subsys_initcall(hwmon_init);
7831236441fSMark M. Hoffman module_exit(hwmon_exit);
7841236441fSMark M. Hoffman 
7851236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
7861236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
7871236441fSMark M. Hoffman MODULE_LICENSE("GPL");
7881236441fSMark M. Hoffman 
789