xref: /linux/drivers/hwmon/hwmon.c (revision 68c0d69dee594e1488aebe12aa50fd79a3e5e5b5)
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 
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)
54d560168bSGuenter Roeck 
55d560168bSGuenter Roeck /*
56d560168bSGuenter Roeck  * Thermal zone information
57d560168bSGuenter Roeck  * In addition to the reference to the hwmon device,
58d560168bSGuenter Roeck  * also provides the sensor index.
59d560168bSGuenter Roeck  */
60d560168bSGuenter Roeck struct hwmon_thermal_data {
61d560168bSGuenter Roeck 	struct hwmon_device *hwdev;	/* Reference to hwmon device */
62d560168bSGuenter Roeck 	int index;			/* sensor index */
63d560168bSGuenter Roeck };
64d560168bSGuenter Roeck 
65bab2243cSGuenter Roeck static ssize_t
662ab0c6c5SJulia Lawall name_show(struct device *dev, struct device_attribute *attr, char *buf)
67bab2243cSGuenter Roeck {
68bab2243cSGuenter Roeck 	return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
69bab2243cSGuenter Roeck }
702ab0c6c5SJulia Lawall static DEVICE_ATTR_RO(name);
71bab2243cSGuenter Roeck 
72bab2243cSGuenter Roeck static struct attribute *hwmon_dev_attrs[] = {
73bab2243cSGuenter Roeck 	&dev_attr_name.attr,
74bab2243cSGuenter Roeck 	NULL
75bab2243cSGuenter Roeck };
76bab2243cSGuenter Roeck 
77bab2243cSGuenter Roeck static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
78bab2243cSGuenter Roeck 					 struct attribute *attr, int n)
79bab2243cSGuenter Roeck {
80bab2243cSGuenter Roeck 	struct device *dev = container_of(kobj, struct device, kobj);
81bab2243cSGuenter Roeck 
82bab2243cSGuenter Roeck 	if (to_hwmon_device(dev)->name == NULL)
83bab2243cSGuenter Roeck 		return 0;
84bab2243cSGuenter Roeck 
85bab2243cSGuenter Roeck 	return attr->mode;
86bab2243cSGuenter Roeck }
87bab2243cSGuenter Roeck 
88524703acSArvind Yadav static const struct attribute_group hwmon_dev_attr_group = {
89bab2243cSGuenter Roeck 	.attrs		= hwmon_dev_attrs,
90bab2243cSGuenter Roeck 	.is_visible	= hwmon_dev_name_is_visible,
91bab2243cSGuenter Roeck };
92bab2243cSGuenter Roeck 
93bab2243cSGuenter Roeck static const struct attribute_group *hwmon_dev_attr_groups[] = {
94bab2243cSGuenter Roeck 	&hwmon_dev_attr_group,
95bab2243cSGuenter Roeck 	NULL
96bab2243cSGuenter Roeck };
97bab2243cSGuenter Roeck 
98bab2243cSGuenter Roeck static void hwmon_dev_release(struct device *dev)
99bab2243cSGuenter Roeck {
100bab2243cSGuenter Roeck 	kfree(to_hwmon_device(dev));
101bab2243cSGuenter Roeck }
102bab2243cSGuenter Roeck 
103bab2243cSGuenter Roeck static struct class hwmon_class = {
104bab2243cSGuenter Roeck 	.name = "hwmon",
105bab2243cSGuenter Roeck 	.owner = THIS_MODULE,
106bab2243cSGuenter Roeck 	.dev_groups = hwmon_dev_attr_groups,
107bab2243cSGuenter Roeck 	.dev_release = hwmon_dev_release,
108bab2243cSGuenter Roeck };
1091236441fSMark M. Hoffman 
1104ca5f468SJonathan Cameron static DEFINE_IDA(hwmon_ida);
1111236441fSMark M. Hoffman 
112d560168bSGuenter Roeck /* Thermal zone handling */
113d560168bSGuenter Roeck 
11486430c1aSGuenter Roeck /*
11586430c1aSGuenter Roeck  * The complex conditional is necessary to avoid a cyclic dependency
11686430c1aSGuenter Roeck  * between hwmon and thermal_sys modules.
11786430c1aSGuenter Roeck  */
11886430c1aSGuenter Roeck #if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) && \
11986430c1aSGuenter Roeck 	(!defined(CONFIG_THERMAL_HWMON) || \
12086430c1aSGuenter Roeck 	 !(defined(MODULE) && IS_MODULE(CONFIG_THERMAL)))
121d560168bSGuenter Roeck static int hwmon_thermal_get_temp(void *data, int *temp)
122d560168bSGuenter Roeck {
123d560168bSGuenter Roeck 	struct hwmon_thermal_data *tdata = data;
124d560168bSGuenter Roeck 	struct hwmon_device *hwdev = tdata->hwdev;
125d560168bSGuenter Roeck 	int ret;
126d560168bSGuenter Roeck 	long t;
127d560168bSGuenter Roeck 
128d560168bSGuenter Roeck 	ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
129d560168bSGuenter Roeck 				     tdata->index, &t);
130d560168bSGuenter Roeck 	if (ret < 0)
131d560168bSGuenter Roeck 		return ret;
132d560168bSGuenter Roeck 
133d560168bSGuenter Roeck 	*temp = t;
134d560168bSGuenter Roeck 
135d560168bSGuenter Roeck 	return 0;
136d560168bSGuenter Roeck }
137d560168bSGuenter Roeck 
138c9920650SJulia Lawall static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
139d560168bSGuenter Roeck 	.get_temp = hwmon_thermal_get_temp,
140d560168bSGuenter Roeck };
141d560168bSGuenter Roeck 
142d560168bSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev,
143d560168bSGuenter Roeck 				    struct hwmon_device *hwdev, int index)
144d560168bSGuenter Roeck {
145d560168bSGuenter Roeck 	struct hwmon_thermal_data *tdata;
14647c332deSLinus Walleij 	struct thermal_zone_device *tzd;
147d560168bSGuenter Roeck 
148d560168bSGuenter Roeck 	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
149d560168bSGuenter Roeck 	if (!tdata)
150d560168bSGuenter Roeck 		return -ENOMEM;
151d560168bSGuenter Roeck 
152d560168bSGuenter Roeck 	tdata->hwdev = hwdev;
153d560168bSGuenter Roeck 	tdata->index = index;
154d560168bSGuenter Roeck 
15547c332deSLinus Walleij 	tzd = devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
156d560168bSGuenter Roeck 						   &hwmon_thermal_ops);
15747c332deSLinus Walleij 	/*
15847c332deSLinus Walleij 	 * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV,
15947c332deSLinus Walleij 	 * so ignore that error but forward any other error.
16047c332deSLinus Walleij 	 */
16147c332deSLinus Walleij 	if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV))
16247c332deSLinus Walleij 		return PTR_ERR(tzd);
163d560168bSGuenter Roeck 
164d560168bSGuenter Roeck 	return 0;
165d560168bSGuenter Roeck }
166d560168bSGuenter Roeck #else
167d560168bSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev,
168d560168bSGuenter Roeck 				    struct hwmon_device *hwdev, int index)
169d560168bSGuenter Roeck {
170d560168bSGuenter Roeck 	return 0;
171d560168bSGuenter Roeck }
17286430c1aSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
173d560168bSGuenter Roeck 
174d560168bSGuenter Roeck /* sysfs attribute management */
175d560168bSGuenter Roeck 
176d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev,
177d560168bSGuenter Roeck 			       struct device_attribute *devattr, char *buf)
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 = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
184d560168bSGuenter Roeck 			       &val);
185d560168bSGuenter Roeck 	if (ret < 0)
186d560168bSGuenter Roeck 		return ret;
187d560168bSGuenter Roeck 
188d560168bSGuenter Roeck 	return sprintf(buf, "%ld\n", val);
189d560168bSGuenter Roeck }
190d560168bSGuenter Roeck 
191e159ab5cSGuenter Roeck static ssize_t hwmon_attr_show_string(struct device *dev,
192e159ab5cSGuenter Roeck 				      struct device_attribute *devattr,
193e159ab5cSGuenter Roeck 				      char *buf)
194e159ab5cSGuenter Roeck {
195e159ab5cSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
1965ba6bcbcSJean Delvare 	const char *s;
197e159ab5cSGuenter Roeck 	int ret;
198e159ab5cSGuenter Roeck 
199e159ab5cSGuenter Roeck 	ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
200e159ab5cSGuenter Roeck 				      hattr->index, &s);
201e159ab5cSGuenter Roeck 	if (ret < 0)
202e159ab5cSGuenter Roeck 		return ret;
203e159ab5cSGuenter Roeck 
204e159ab5cSGuenter Roeck 	return sprintf(buf, "%s\n", s);
205e159ab5cSGuenter Roeck }
206e159ab5cSGuenter Roeck 
207d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev,
208d560168bSGuenter Roeck 				struct device_attribute *devattr,
209d560168bSGuenter Roeck 				const char *buf, size_t count)
210d560168bSGuenter Roeck {
211d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
212d560168bSGuenter Roeck 	long val;
213d560168bSGuenter Roeck 	int ret;
214d560168bSGuenter Roeck 
215d560168bSGuenter Roeck 	ret = kstrtol(buf, 10, &val);
216d560168bSGuenter Roeck 	if (ret < 0)
217d560168bSGuenter Roeck 		return ret;
218d560168bSGuenter Roeck 
219d560168bSGuenter Roeck 	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
220d560168bSGuenter Roeck 				val);
221d560168bSGuenter Roeck 	if (ret < 0)
222d560168bSGuenter Roeck 		return ret;
223d560168bSGuenter Roeck 
224d560168bSGuenter Roeck 	return count;
225d560168bSGuenter Roeck }
226d560168bSGuenter Roeck 
227d560168bSGuenter Roeck static int hwmon_attr_base(enum hwmon_sensor_types type)
228d560168bSGuenter Roeck {
229d560168bSGuenter Roeck 	if (type == hwmon_in)
230d560168bSGuenter Roeck 		return 0;
231d560168bSGuenter Roeck 	return 1;
232d560168bSGuenter Roeck }
233d560168bSGuenter Roeck 
234e159ab5cSGuenter Roeck static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
235e159ab5cSGuenter Roeck {
236e159ab5cSGuenter Roeck 	return (type == hwmon_temp && attr == hwmon_temp_label) ||
237e159ab5cSGuenter Roeck 	       (type == hwmon_in && attr == hwmon_in_label) ||
238e159ab5cSGuenter Roeck 	       (type == hwmon_curr && attr == hwmon_curr_label) ||
239e159ab5cSGuenter Roeck 	       (type == hwmon_power && attr == hwmon_power_label) ||
240e159ab5cSGuenter Roeck 	       (type == hwmon_energy && attr == hwmon_energy_label) ||
241e159ab5cSGuenter Roeck 	       (type == hwmon_humidity && attr == hwmon_humidity_label) ||
242e159ab5cSGuenter Roeck 	       (type == hwmon_fan && attr == hwmon_fan_label);
243e159ab5cSGuenter Roeck }
244e159ab5cSGuenter Roeck 
245d560168bSGuenter Roeck static struct attribute *hwmon_genattr(struct device *dev,
246d560168bSGuenter Roeck 				       const void *drvdata,
247d560168bSGuenter Roeck 				       enum hwmon_sensor_types type,
248d560168bSGuenter Roeck 				       u32 attr,
249d560168bSGuenter Roeck 				       int index,
250d560168bSGuenter Roeck 				       const char *template,
251d560168bSGuenter Roeck 				       const struct hwmon_ops *ops)
252d560168bSGuenter Roeck {
253d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr;
254d560168bSGuenter Roeck 	struct device_attribute *dattr;
255d560168bSGuenter Roeck 	struct attribute *a;
256d560168bSGuenter Roeck 	umode_t mode;
257d560168bSGuenter Roeck 	char *name;
258e159ab5cSGuenter Roeck 	bool is_string = is_string_attr(type, attr);
259d560168bSGuenter Roeck 
260d560168bSGuenter Roeck 	/* The attribute is invisible if there is no template string */
261d560168bSGuenter Roeck 	if (!template)
262d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
263d560168bSGuenter Roeck 
264d560168bSGuenter Roeck 	mode = ops->is_visible(drvdata, type, attr, index);
265d560168bSGuenter Roeck 	if (!mode)
266d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
267d560168bSGuenter Roeck 
268e159ab5cSGuenter Roeck 	if ((mode & S_IRUGO) && ((is_string && !ops->read_string) ||
269e159ab5cSGuenter Roeck 				 (!is_string && !ops->read)))
270d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
271d560168bSGuenter Roeck 	if ((mode & S_IWUGO) && !ops->write)
272d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
273d560168bSGuenter Roeck 
274d560168bSGuenter Roeck 	hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
275d560168bSGuenter Roeck 	if (!hattr)
276d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
277d560168bSGuenter Roeck 
2783a412d5eSGuenter Roeck 	if (type == hwmon_chip) {
2793a412d5eSGuenter Roeck 		name = (char *)template;
2803a412d5eSGuenter Roeck 	} else {
2813a412d5eSGuenter Roeck 		scnprintf(hattr->name, sizeof(hattr->name), template,
2823a412d5eSGuenter Roeck 			  index + hwmon_attr_base(type));
2833a412d5eSGuenter Roeck 		name = hattr->name;
2843a412d5eSGuenter Roeck 	}
2853a412d5eSGuenter Roeck 
286d560168bSGuenter Roeck 	hattr->type = type;
287d560168bSGuenter Roeck 	hattr->attr = attr;
288d560168bSGuenter Roeck 	hattr->index = index;
289d560168bSGuenter Roeck 	hattr->ops = ops;
290d560168bSGuenter Roeck 
291d560168bSGuenter Roeck 	dattr = &hattr->dev_attr;
292e159ab5cSGuenter Roeck 	dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
293d560168bSGuenter Roeck 	dattr->store = hwmon_attr_store;
294d560168bSGuenter Roeck 
295d560168bSGuenter Roeck 	a = &dattr->attr;
296d560168bSGuenter Roeck 	sysfs_attr_init(a);
297d560168bSGuenter Roeck 	a->name = name;
298d560168bSGuenter Roeck 	a->mode = mode;
299d560168bSGuenter Roeck 
300d560168bSGuenter Roeck 	return a;
301d560168bSGuenter Roeck }
302d560168bSGuenter Roeck 
303f4d325d5SGuenter Roeck /*
304f4d325d5SGuenter Roeck  * Chip attributes are not attribute templates but actual sysfs attributes.
305f4d325d5SGuenter Roeck  * See hwmon_genattr() for special handling.
306f4d325d5SGuenter Roeck  */
307f4d325d5SGuenter Roeck static const char * const hwmon_chip_attrs[] = {
308d560168bSGuenter Roeck 	[hwmon_chip_temp_reset_history] = "temp_reset_history",
30900d616cfSGuenter Roeck 	[hwmon_chip_in_reset_history] = "in_reset_history",
3109b26947cSGuenter Roeck 	[hwmon_chip_curr_reset_history] = "curr_reset_history",
311b308f5c7SGuenter Roeck 	[hwmon_chip_power_reset_history] = "power_reset_history",
312d560168bSGuenter Roeck 	[hwmon_chip_update_interval] = "update_interval",
313d560168bSGuenter Roeck 	[hwmon_chip_alarms] = "alarms",
314d560168bSGuenter Roeck };
315d560168bSGuenter Roeck 
316d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = {
317d560168bSGuenter Roeck 	[hwmon_temp_input] = "temp%d_input",
318d560168bSGuenter Roeck 	[hwmon_temp_type] = "temp%d_type",
319d560168bSGuenter Roeck 	[hwmon_temp_lcrit] = "temp%d_lcrit",
320d560168bSGuenter Roeck 	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
321d560168bSGuenter Roeck 	[hwmon_temp_min] = "temp%d_min",
322d560168bSGuenter Roeck 	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
323d560168bSGuenter Roeck 	[hwmon_temp_max] = "temp%d_max",
324d560168bSGuenter Roeck 	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
325d560168bSGuenter Roeck 	[hwmon_temp_crit] = "temp%d_crit",
326d560168bSGuenter Roeck 	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
327d560168bSGuenter Roeck 	[hwmon_temp_emergency] = "temp%d_emergency",
328d560168bSGuenter Roeck 	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
329d560168bSGuenter Roeck 	[hwmon_temp_alarm] = "temp%d_alarm",
330d560168bSGuenter Roeck 	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
331d560168bSGuenter Roeck 	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
332d560168bSGuenter Roeck 	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
333d560168bSGuenter Roeck 	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
334d560168bSGuenter Roeck 	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
335d560168bSGuenter Roeck 	[hwmon_temp_fault] = "temp%d_fault",
336d560168bSGuenter Roeck 	[hwmon_temp_offset] = "temp%d_offset",
337d560168bSGuenter Roeck 	[hwmon_temp_label] = "temp%d_label",
338d560168bSGuenter Roeck 	[hwmon_temp_lowest] = "temp%d_lowest",
339d560168bSGuenter Roeck 	[hwmon_temp_highest] = "temp%d_highest",
340d560168bSGuenter Roeck 	[hwmon_temp_reset_history] = "temp%d_reset_history",
341d560168bSGuenter Roeck };
342d560168bSGuenter Roeck 
34300d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = {
34400d616cfSGuenter Roeck 	[hwmon_in_input] = "in%d_input",
34500d616cfSGuenter Roeck 	[hwmon_in_min] = "in%d_min",
34600d616cfSGuenter Roeck 	[hwmon_in_max] = "in%d_max",
34700d616cfSGuenter Roeck 	[hwmon_in_lcrit] = "in%d_lcrit",
34800d616cfSGuenter Roeck 	[hwmon_in_crit] = "in%d_crit",
34900d616cfSGuenter Roeck 	[hwmon_in_average] = "in%d_average",
35000d616cfSGuenter Roeck 	[hwmon_in_lowest] = "in%d_lowest",
35100d616cfSGuenter Roeck 	[hwmon_in_highest] = "in%d_highest",
35200d616cfSGuenter Roeck 	[hwmon_in_reset_history] = "in%d_reset_history",
35300d616cfSGuenter Roeck 	[hwmon_in_label] = "in%d_label",
35400d616cfSGuenter Roeck 	[hwmon_in_alarm] = "in%d_alarm",
35500d616cfSGuenter Roeck 	[hwmon_in_min_alarm] = "in%d_min_alarm",
35600d616cfSGuenter Roeck 	[hwmon_in_max_alarm] = "in%d_max_alarm",
35700d616cfSGuenter Roeck 	[hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
35800d616cfSGuenter Roeck 	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
359*68c0d69dSNicolin Chen 	[hwmon_in_enable] = "in%d_enable",
36000d616cfSGuenter Roeck };
36100d616cfSGuenter Roeck 
3629b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = {
3639b26947cSGuenter Roeck 	[hwmon_curr_input] = "curr%d_input",
3649b26947cSGuenter Roeck 	[hwmon_curr_min] = "curr%d_min",
3659b26947cSGuenter Roeck 	[hwmon_curr_max] = "curr%d_max",
3669b26947cSGuenter Roeck 	[hwmon_curr_lcrit] = "curr%d_lcrit",
3679b26947cSGuenter Roeck 	[hwmon_curr_crit] = "curr%d_crit",
3689b26947cSGuenter Roeck 	[hwmon_curr_average] = "curr%d_average",
3699b26947cSGuenter Roeck 	[hwmon_curr_lowest] = "curr%d_lowest",
3709b26947cSGuenter Roeck 	[hwmon_curr_highest] = "curr%d_highest",
3719b26947cSGuenter Roeck 	[hwmon_curr_reset_history] = "curr%d_reset_history",
3729b26947cSGuenter Roeck 	[hwmon_curr_label] = "curr%d_label",
3739b26947cSGuenter Roeck 	[hwmon_curr_alarm] = "curr%d_alarm",
3749b26947cSGuenter Roeck 	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
3759b26947cSGuenter Roeck 	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
3769b26947cSGuenter Roeck 	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
3779b26947cSGuenter Roeck 	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
3789b26947cSGuenter Roeck };
3799b26947cSGuenter Roeck 
380b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = {
381b308f5c7SGuenter Roeck 	[hwmon_power_average] = "power%d_average",
382b308f5c7SGuenter Roeck 	[hwmon_power_average_interval] = "power%d_average_interval",
383b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_max] = "power%d_interval_max",
384b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_min] = "power%d_interval_min",
385b308f5c7SGuenter Roeck 	[hwmon_power_average_highest] = "power%d_average_highest",
386b308f5c7SGuenter Roeck 	[hwmon_power_average_lowest] = "power%d_average_lowest",
387b308f5c7SGuenter Roeck 	[hwmon_power_average_max] = "power%d_average_max",
388b308f5c7SGuenter Roeck 	[hwmon_power_average_min] = "power%d_average_min",
389b308f5c7SGuenter Roeck 	[hwmon_power_input] = "power%d_input",
390b308f5c7SGuenter Roeck 	[hwmon_power_input_highest] = "power%d_input_highest",
391b308f5c7SGuenter Roeck 	[hwmon_power_input_lowest] = "power%d_input_lowest",
392b308f5c7SGuenter Roeck 	[hwmon_power_reset_history] = "power%d_reset_history",
393b308f5c7SGuenter Roeck 	[hwmon_power_accuracy] = "power%d_accuracy",
394b308f5c7SGuenter Roeck 	[hwmon_power_cap] = "power%d_cap",
395b308f5c7SGuenter Roeck 	[hwmon_power_cap_hyst] = "power%d_cap_hyst",
396b308f5c7SGuenter Roeck 	[hwmon_power_cap_max] = "power%d_cap_max",
397b308f5c7SGuenter Roeck 	[hwmon_power_cap_min] = "power%d_cap_min",
398aa7f29b0SAndrew Lunn 	[hwmon_power_min] = "power%d_min",
399b308f5c7SGuenter Roeck 	[hwmon_power_max] = "power%d_max",
400aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit] = "power%d_lcrit",
401b308f5c7SGuenter Roeck 	[hwmon_power_crit] = "power%d_crit",
402b308f5c7SGuenter Roeck 	[hwmon_power_label] = "power%d_label",
403b308f5c7SGuenter Roeck 	[hwmon_power_alarm] = "power%d_alarm",
404b308f5c7SGuenter Roeck 	[hwmon_power_cap_alarm] = "power%d_cap_alarm",
405aa7f29b0SAndrew Lunn 	[hwmon_power_min_alarm] = "power%d_min_alarm",
406b308f5c7SGuenter Roeck 	[hwmon_power_max_alarm] = "power%d_max_alarm",
407aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
408b308f5c7SGuenter Roeck 	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
409b308f5c7SGuenter Roeck };
410b308f5c7SGuenter Roeck 
4116bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = {
4126bfcca44SGuenter Roeck 	[hwmon_energy_input] = "energy%d_input",
4136bfcca44SGuenter Roeck 	[hwmon_energy_label] = "energy%d_label",
4146bfcca44SGuenter Roeck };
4156bfcca44SGuenter Roeck 
4166bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = {
4176bfcca44SGuenter Roeck 	[hwmon_humidity_input] = "humidity%d_input",
4186bfcca44SGuenter Roeck 	[hwmon_humidity_label] = "humidity%d_label",
4196bfcca44SGuenter Roeck 	[hwmon_humidity_min] = "humidity%d_min",
4206bfcca44SGuenter Roeck 	[hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
4216bfcca44SGuenter Roeck 	[hwmon_humidity_max] = "humidity%d_max",
4226bfcca44SGuenter Roeck 	[hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
4236bfcca44SGuenter Roeck 	[hwmon_humidity_alarm] = "humidity%d_alarm",
4246bfcca44SGuenter Roeck 	[hwmon_humidity_fault] = "humidity%d_fault",
4256bfcca44SGuenter Roeck };
4266bfcca44SGuenter Roeck 
4278faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = {
4288faee73fSGuenter Roeck 	[hwmon_fan_input] = "fan%d_input",
4298faee73fSGuenter Roeck 	[hwmon_fan_label] = "fan%d_label",
4308faee73fSGuenter Roeck 	[hwmon_fan_min] = "fan%d_min",
4318faee73fSGuenter Roeck 	[hwmon_fan_max] = "fan%d_max",
4328faee73fSGuenter Roeck 	[hwmon_fan_div] = "fan%d_div",
4338faee73fSGuenter Roeck 	[hwmon_fan_pulses] = "fan%d_pulses",
4348faee73fSGuenter Roeck 	[hwmon_fan_target] = "fan%d_target",
4358faee73fSGuenter Roeck 	[hwmon_fan_alarm] = "fan%d_alarm",
4368faee73fSGuenter Roeck 	[hwmon_fan_min_alarm] = "fan%d_min_alarm",
4378faee73fSGuenter Roeck 	[hwmon_fan_max_alarm] = "fan%d_max_alarm",
4388faee73fSGuenter Roeck 	[hwmon_fan_fault] = "fan%d_fault",
4398faee73fSGuenter Roeck };
4408faee73fSGuenter Roeck 
441f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = {
442f9f7bb3aSGuenter Roeck 	[hwmon_pwm_input] = "pwm%d",
443f9f7bb3aSGuenter Roeck 	[hwmon_pwm_enable] = "pwm%d_enable",
444f9f7bb3aSGuenter Roeck 	[hwmon_pwm_mode] = "pwm%d_mode",
445f9f7bb3aSGuenter Roeck 	[hwmon_pwm_freq] = "pwm%d_freq",
446f9f7bb3aSGuenter Roeck };
447f9f7bb3aSGuenter Roeck 
448d560168bSGuenter Roeck static const char * const *__templates[] = {
449f4d325d5SGuenter Roeck 	[hwmon_chip] = hwmon_chip_attrs,
450d560168bSGuenter Roeck 	[hwmon_temp] = hwmon_temp_attr_templates,
45100d616cfSGuenter Roeck 	[hwmon_in] = hwmon_in_attr_templates,
4529b26947cSGuenter Roeck 	[hwmon_curr] = hwmon_curr_attr_templates,
453b308f5c7SGuenter Roeck 	[hwmon_power] = hwmon_power_attr_templates,
4546bfcca44SGuenter Roeck 	[hwmon_energy] = hwmon_energy_attr_templates,
4556bfcca44SGuenter Roeck 	[hwmon_humidity] = hwmon_humidity_attr_templates,
4568faee73fSGuenter Roeck 	[hwmon_fan] = hwmon_fan_attr_templates,
457f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = hwmon_pwm_attr_templates,
458d560168bSGuenter Roeck };
459d560168bSGuenter Roeck 
460d560168bSGuenter Roeck static const int __templates_size[] = {
461f4d325d5SGuenter Roeck 	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
462d560168bSGuenter Roeck 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
46300d616cfSGuenter Roeck 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
4649b26947cSGuenter Roeck 	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
465b308f5c7SGuenter Roeck 	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
4666bfcca44SGuenter Roeck 	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
4676bfcca44SGuenter Roeck 	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
4688faee73fSGuenter Roeck 	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
469f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
470d560168bSGuenter Roeck };
471d560168bSGuenter Roeck 
472d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
473d560168bSGuenter Roeck {
474d560168bSGuenter Roeck 	int i, n;
475d560168bSGuenter Roeck 
476d560168bSGuenter Roeck 	for (i = n = 0; info->config[i]; i++)
477d560168bSGuenter Roeck 		n += hweight32(info->config[i]);
478d560168bSGuenter Roeck 
479d560168bSGuenter Roeck 	return n;
480d560168bSGuenter Roeck }
481d560168bSGuenter Roeck 
482d560168bSGuenter Roeck static int hwmon_genattrs(struct device *dev,
483d560168bSGuenter Roeck 			  const void *drvdata,
484d560168bSGuenter Roeck 			  struct attribute **attrs,
485d560168bSGuenter Roeck 			  const struct hwmon_ops *ops,
486d560168bSGuenter Roeck 			  const struct hwmon_channel_info *info)
487d560168bSGuenter Roeck {
488d560168bSGuenter Roeck 	const char * const *templates;
489d560168bSGuenter Roeck 	int template_size;
490d560168bSGuenter Roeck 	int i, aindex = 0;
491d560168bSGuenter Roeck 
492d560168bSGuenter Roeck 	if (info->type >= ARRAY_SIZE(__templates))
493d560168bSGuenter Roeck 		return -EINVAL;
494d560168bSGuenter Roeck 
495d560168bSGuenter Roeck 	templates = __templates[info->type];
496d560168bSGuenter Roeck 	template_size = __templates_size[info->type];
497d560168bSGuenter Roeck 
498d560168bSGuenter Roeck 	for (i = 0; info->config[i]; i++) {
499d560168bSGuenter Roeck 		u32 attr_mask = info->config[i];
500d560168bSGuenter Roeck 		u32 attr;
501d560168bSGuenter Roeck 
502d560168bSGuenter Roeck 		while (attr_mask) {
503d560168bSGuenter Roeck 			struct attribute *a;
504d560168bSGuenter Roeck 
505d560168bSGuenter Roeck 			attr = __ffs(attr_mask);
506d560168bSGuenter Roeck 			attr_mask &= ~BIT(attr);
507d560168bSGuenter Roeck 			if (attr >= template_size)
508d560168bSGuenter Roeck 				return -EINVAL;
509d560168bSGuenter Roeck 			a = hwmon_genattr(dev, drvdata, info->type, attr, i,
510d560168bSGuenter Roeck 					  templates[attr], ops);
511d560168bSGuenter Roeck 			if (IS_ERR(a)) {
512d560168bSGuenter Roeck 				if (PTR_ERR(a) != -ENOENT)
513d560168bSGuenter Roeck 					return PTR_ERR(a);
514d560168bSGuenter Roeck 				continue;
515d560168bSGuenter Roeck 			}
516d560168bSGuenter Roeck 			attrs[aindex++] = a;
517d560168bSGuenter Roeck 		}
518d560168bSGuenter Roeck 	}
519d560168bSGuenter Roeck 	return aindex;
520d560168bSGuenter Roeck }
521d560168bSGuenter Roeck 
522d560168bSGuenter Roeck static struct attribute **
523d560168bSGuenter Roeck __hwmon_create_attrs(struct device *dev, const void *drvdata,
524d560168bSGuenter Roeck 		     const struct hwmon_chip_info *chip)
525d560168bSGuenter Roeck {
526d560168bSGuenter Roeck 	int ret, i, aindex = 0, nattrs = 0;
527d560168bSGuenter Roeck 	struct attribute **attrs;
528d560168bSGuenter Roeck 
529d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++)
530d560168bSGuenter Roeck 		nattrs += hwmon_num_channel_attrs(chip->info[i]);
531d560168bSGuenter Roeck 
532d560168bSGuenter Roeck 	if (nattrs == 0)
533d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
534d560168bSGuenter Roeck 
535d560168bSGuenter Roeck 	attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
536d560168bSGuenter Roeck 	if (!attrs)
537d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
538d560168bSGuenter Roeck 
539d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++) {
540d560168bSGuenter Roeck 		ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
541d560168bSGuenter Roeck 				     chip->info[i]);
542d560168bSGuenter Roeck 		if (ret < 0)
543d560168bSGuenter Roeck 			return ERR_PTR(ret);
544d560168bSGuenter Roeck 		aindex += ret;
545d560168bSGuenter Roeck 	}
546d560168bSGuenter Roeck 
547d560168bSGuenter Roeck 	return attrs;
548d560168bSGuenter Roeck }
549d560168bSGuenter Roeck 
550d560168bSGuenter Roeck static struct device *
551d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
552d560168bSGuenter Roeck 			const struct hwmon_chip_info *chip,
553d560168bSGuenter Roeck 			const struct attribute_group **groups)
554d560168bSGuenter Roeck {
555d560168bSGuenter Roeck 	struct hwmon_device *hwdev;
556d560168bSGuenter Roeck 	struct device *hdev;
557d560168bSGuenter Roeck 	int i, j, err, id;
558d560168bSGuenter Roeck 
55974d3b641SGuenter Roeck 	/* Complain about invalid characters in hwmon name attribute */
560d560168bSGuenter Roeck 	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
56174d3b641SGuenter Roeck 		dev_warn(dev,
56274d3b641SGuenter Roeck 			 "hwmon: '%s' is not a valid name attribute, please fix\n",
56374d3b641SGuenter Roeck 			 name);
564d560168bSGuenter Roeck 
565d560168bSGuenter Roeck 	id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
566d560168bSGuenter Roeck 	if (id < 0)
567d560168bSGuenter Roeck 		return ERR_PTR(id);
568d560168bSGuenter Roeck 
569d560168bSGuenter Roeck 	hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
570d560168bSGuenter Roeck 	if (hwdev == NULL) {
571d560168bSGuenter Roeck 		err = -ENOMEM;
572d560168bSGuenter Roeck 		goto ida_remove;
573d560168bSGuenter Roeck 	}
574d560168bSGuenter Roeck 
575d560168bSGuenter Roeck 	hdev = &hwdev->dev;
576d560168bSGuenter Roeck 
577239552f4SGuenter Roeck 	if (chip) {
578d560168bSGuenter Roeck 		struct attribute **attrs;
579b2a4cc3aSGuenter Roeck 		int ngroups = 2; /* terminating NULL plus &hwdev->groups */
580d560168bSGuenter Roeck 
581d560168bSGuenter Roeck 		if (groups)
582d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
583d560168bSGuenter Roeck 				ngroups++;
584d560168bSGuenter Roeck 
585d560168bSGuenter Roeck 		hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
586d560168bSGuenter Roeck 					     GFP_KERNEL);
58738d8ed65SColin Ian King 		if (!hwdev->groups) {
58838d8ed65SColin Ian King 			err = -ENOMEM;
58938d8ed65SColin Ian King 			goto free_hwmon;
59038d8ed65SColin Ian King 		}
591d560168bSGuenter Roeck 
592d560168bSGuenter Roeck 		attrs = __hwmon_create_attrs(dev, drvdata, chip);
593d560168bSGuenter Roeck 		if (IS_ERR(attrs)) {
594d560168bSGuenter Roeck 			err = PTR_ERR(attrs);
595d560168bSGuenter Roeck 			goto free_hwmon;
596d560168bSGuenter Roeck 		}
597d560168bSGuenter Roeck 
598d560168bSGuenter Roeck 		hwdev->group.attrs = attrs;
599d560168bSGuenter Roeck 		ngroups = 0;
600d560168bSGuenter Roeck 		hwdev->groups[ngroups++] = &hwdev->group;
601d560168bSGuenter Roeck 
602d560168bSGuenter Roeck 		if (groups) {
603d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
604d560168bSGuenter Roeck 				hwdev->groups[ngroups++] = groups[i];
605d560168bSGuenter Roeck 		}
606d560168bSGuenter Roeck 
607d560168bSGuenter Roeck 		hdev->groups = hwdev->groups;
608d560168bSGuenter Roeck 	} else {
609d560168bSGuenter Roeck 		hdev->groups = groups;
610d560168bSGuenter Roeck 	}
611d560168bSGuenter Roeck 
612d560168bSGuenter Roeck 	hwdev->name = name;
613d560168bSGuenter Roeck 	hdev->class = &hwmon_class;
614d560168bSGuenter Roeck 	hdev->parent = dev;
615d560168bSGuenter Roeck 	hdev->of_node = dev ? dev->of_node : NULL;
616d560168bSGuenter Roeck 	hwdev->chip = chip;
617d560168bSGuenter Roeck 	dev_set_drvdata(hdev, drvdata);
618d560168bSGuenter Roeck 	dev_set_name(hdev, HWMON_ID_FORMAT, id);
619d560168bSGuenter Roeck 	err = device_register(hdev);
620d560168bSGuenter Roeck 	if (err)
621d560168bSGuenter Roeck 		goto free_hwmon;
622d560168bSGuenter Roeck 
623319fe159SGuenter Roeck 	if (dev && chip && chip->ops->read &&
624d560168bSGuenter Roeck 	    chip->info[0]->type == hwmon_chip &&
625d560168bSGuenter Roeck 	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
626d560168bSGuenter Roeck 		const struct hwmon_channel_info **info = chip->info;
627d560168bSGuenter Roeck 
628d560168bSGuenter Roeck 		for (i = 1; info[i]; i++) {
629d560168bSGuenter Roeck 			if (info[i]->type != hwmon_temp)
630d560168bSGuenter Roeck 				continue;
631d560168bSGuenter Roeck 
632d560168bSGuenter Roeck 			for (j = 0; info[i]->config[j]; j++) {
633d560168bSGuenter Roeck 				if (!chip->ops->is_visible(drvdata, hwmon_temp,
634d560168bSGuenter Roeck 							   hwmon_temp_input, j))
635d560168bSGuenter Roeck 					continue;
63647c332deSLinus Walleij 				if (info[i]->config[j] & HWMON_T_INPUT) {
63747c332deSLinus Walleij 					err = hwmon_thermal_add_sensor(dev,
63847c332deSLinus Walleij 								hwdev, j);
63947c332deSLinus Walleij 					if (err)
64047c332deSLinus Walleij 						goto free_device;
64147c332deSLinus Walleij 				}
642d560168bSGuenter Roeck 			}
643d560168bSGuenter Roeck 		}
644d560168bSGuenter Roeck 	}
645d560168bSGuenter Roeck 
646d560168bSGuenter Roeck 	return hdev;
647d560168bSGuenter Roeck 
64847c332deSLinus Walleij free_device:
64947c332deSLinus Walleij 	device_unregister(hdev);
650d560168bSGuenter Roeck free_hwmon:
651d560168bSGuenter Roeck 	kfree(hwdev);
652d560168bSGuenter Roeck ida_remove:
653d560168bSGuenter Roeck 	ida_simple_remove(&hwmon_ida, id);
654d560168bSGuenter Roeck 	return ERR_PTR(err);
655d560168bSGuenter Roeck }
656d560168bSGuenter Roeck 
6571236441fSMark M. Hoffman /**
658bab2243cSGuenter Roeck  * hwmon_device_register_with_groups - register w/ hwmon
659bab2243cSGuenter Roeck  * @dev: the parent device
660bab2243cSGuenter Roeck  * @name: hwmon name attribute
661bab2243cSGuenter Roeck  * @drvdata: driver data to attach to created device
662bab2243cSGuenter Roeck  * @groups: List of attribute groups to create
663bab2243cSGuenter Roeck  *
664bab2243cSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
665bab2243cSGuenter Roeck  * longer needed.
666bab2243cSGuenter Roeck  *
667bab2243cSGuenter Roeck  * Returns the pointer to the new device.
668bab2243cSGuenter Roeck  */
669bab2243cSGuenter Roeck struct device *
670bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name,
671bab2243cSGuenter Roeck 				  void *drvdata,
672bab2243cSGuenter Roeck 				  const struct attribute_group **groups)
673bab2243cSGuenter Roeck {
6748353863aSGuenter Roeck 	if (!name)
6758353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
6768353863aSGuenter Roeck 
677d560168bSGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
678bab2243cSGuenter Roeck }
679bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
680bab2243cSGuenter Roeck 
681bab2243cSGuenter Roeck /**
682d560168bSGuenter Roeck  * hwmon_device_register_with_info - register w/ hwmon
683d560168bSGuenter Roeck  * @dev: the parent device
684d560168bSGuenter Roeck  * @name: hwmon name attribute
685d560168bSGuenter Roeck  * @drvdata: driver data to attach to created device
6863870945aSGuenter Roeck  * @chip: pointer to hwmon chip information
687848ba0a2SGuenter Roeck  * @extra_groups: pointer to list of additional non-standard attribute groups
688d560168bSGuenter Roeck  *
689d560168bSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
690d560168bSGuenter Roeck  * longer needed.
691d560168bSGuenter Roeck  *
692d560168bSGuenter Roeck  * Returns the pointer to the new device.
693d560168bSGuenter Roeck  */
694d560168bSGuenter Roeck struct device *
695d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name,
696d560168bSGuenter Roeck 				void *drvdata,
697d560168bSGuenter Roeck 				const struct hwmon_chip_info *chip,
698848ba0a2SGuenter Roeck 				const struct attribute_group **extra_groups)
699d560168bSGuenter Roeck {
7008353863aSGuenter Roeck 	if (!name)
7018353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
7028353863aSGuenter Roeck 
703239552f4SGuenter Roeck 	if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
704d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
705d560168bSGuenter Roeck 
70659df4f4eSLucas Magasweran 	if (chip && !dev)
70759df4f4eSLucas Magasweran 		return ERR_PTR(-EINVAL);
70859df4f4eSLucas Magasweran 
709848ba0a2SGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
710d560168bSGuenter Roeck }
711d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
712d560168bSGuenter Roeck 
713d560168bSGuenter Roeck /**
7141beeffe4STony Jones  * hwmon_device_register - register w/ hwmon
7151236441fSMark M. Hoffman  * @dev: the device to register
7161236441fSMark M. Hoffman  *
7171beeffe4STony Jones  * hwmon_device_unregister() must be called when the device is no
7181236441fSMark M. Hoffman  * longer needed.
7191236441fSMark M. Hoffman  *
7201beeffe4STony Jones  * Returns the pointer to the new device.
7211236441fSMark M. Hoffman  */
7221beeffe4STony Jones struct device *hwmon_device_register(struct device *dev)
7231236441fSMark M. Hoffman {
724af1bd36cSGuenter Roeck 	dev_warn(dev,
725af1bd36cSGuenter Roeck 		 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
726af1bd36cSGuenter Roeck 
7278353863aSGuenter Roeck 	return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
7281236441fSMark M. Hoffman }
729839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register);
7301236441fSMark M. Hoffman 
7311236441fSMark M. Hoffman /**
7321236441fSMark M. Hoffman  * hwmon_device_unregister - removes the previously registered class device
7331236441fSMark M. Hoffman  *
7341beeffe4STony Jones  * @dev: the class device to destroy
7351236441fSMark M. Hoffman  */
7361beeffe4STony Jones void hwmon_device_unregister(struct device *dev)
7371236441fSMark M. Hoffman {
7381236441fSMark M. Hoffman 	int id;
7391236441fSMark M. Hoffman 
740739cf3a2SKay Sievers 	if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
7411beeffe4STony Jones 		device_unregister(dev);
7424ca5f468SJonathan Cameron 		ida_simple_remove(&hwmon_ida, id);
7431236441fSMark M. Hoffman 	} else
7441beeffe4STony Jones 		dev_dbg(dev->parent,
7451236441fSMark M. Hoffman 			"hwmon_device_unregister() failed: bad class ID!\n");
7461236441fSMark M. Hoffman }
747839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister);
7481236441fSMark M. Hoffman 
74974188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res)
75074188cbaSGuenter Roeck {
75174188cbaSGuenter Roeck 	struct device *hwdev = *(struct device **)res;
75274188cbaSGuenter Roeck 
75374188cbaSGuenter Roeck 	hwmon_device_unregister(hwdev);
75474188cbaSGuenter Roeck }
75574188cbaSGuenter Roeck 
75674188cbaSGuenter Roeck /**
75774188cbaSGuenter Roeck  * devm_hwmon_device_register_with_groups - register w/ hwmon
75874188cbaSGuenter Roeck  * @dev: the parent device
75974188cbaSGuenter Roeck  * @name: hwmon name attribute
76074188cbaSGuenter Roeck  * @drvdata: driver data to attach to created device
76174188cbaSGuenter Roeck  * @groups: List of attribute groups to create
76274188cbaSGuenter Roeck  *
76374188cbaSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
76474188cbaSGuenter Roeck  * unregistered with the parent device.
76574188cbaSGuenter Roeck  */
76674188cbaSGuenter Roeck struct device *
76774188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
76874188cbaSGuenter Roeck 				       void *drvdata,
76974188cbaSGuenter Roeck 				       const struct attribute_group **groups)
77074188cbaSGuenter Roeck {
77174188cbaSGuenter Roeck 	struct device **ptr, *hwdev;
77274188cbaSGuenter Roeck 
77374188cbaSGuenter Roeck 	if (!dev)
77474188cbaSGuenter Roeck 		return ERR_PTR(-EINVAL);
77574188cbaSGuenter Roeck 
77674188cbaSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
77774188cbaSGuenter Roeck 	if (!ptr)
77874188cbaSGuenter Roeck 		return ERR_PTR(-ENOMEM);
77974188cbaSGuenter Roeck 
78074188cbaSGuenter Roeck 	hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
78174188cbaSGuenter Roeck 	if (IS_ERR(hwdev))
78274188cbaSGuenter Roeck 		goto error;
78374188cbaSGuenter Roeck 
78474188cbaSGuenter Roeck 	*ptr = hwdev;
78574188cbaSGuenter Roeck 	devres_add(dev, ptr);
78674188cbaSGuenter Roeck 	return hwdev;
78774188cbaSGuenter Roeck 
78874188cbaSGuenter Roeck error:
78974188cbaSGuenter Roeck 	devres_free(ptr);
79074188cbaSGuenter Roeck 	return hwdev;
79174188cbaSGuenter Roeck }
79274188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
79374188cbaSGuenter Roeck 
794d560168bSGuenter Roeck /**
795d560168bSGuenter Roeck  * devm_hwmon_device_register_with_info - register w/ hwmon
796d560168bSGuenter Roeck  * @dev:	the parent device
797d560168bSGuenter Roeck  * @name:	hwmon name attribute
798d560168bSGuenter Roeck  * @drvdata:	driver data to attach to created device
7993870945aSGuenter Roeck  * @chip:	pointer to hwmon chip information
8003870945aSGuenter Roeck  * @groups:	pointer to list of driver specific attribute groups
801d560168bSGuenter Roeck  *
802d560168bSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
803d560168bSGuenter Roeck  * unregistered with the parent device.
804d560168bSGuenter Roeck  */
805d560168bSGuenter Roeck struct device *
806d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name,
807d560168bSGuenter Roeck 				     void *drvdata,
808d560168bSGuenter Roeck 				     const struct hwmon_chip_info *chip,
809d560168bSGuenter Roeck 				     const struct attribute_group **groups)
810d560168bSGuenter Roeck {
811d560168bSGuenter Roeck 	struct device **ptr, *hwdev;
812d560168bSGuenter Roeck 
813d560168bSGuenter Roeck 	if (!dev)
814d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
815d560168bSGuenter Roeck 
816d560168bSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
817d560168bSGuenter Roeck 	if (!ptr)
818d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
819d560168bSGuenter Roeck 
820d560168bSGuenter Roeck 	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
821d560168bSGuenter Roeck 						groups);
822d560168bSGuenter Roeck 	if (IS_ERR(hwdev))
823d560168bSGuenter Roeck 		goto error;
824d560168bSGuenter Roeck 
825d560168bSGuenter Roeck 	*ptr = hwdev;
826d560168bSGuenter Roeck 	devres_add(dev, ptr);
827d560168bSGuenter Roeck 
828d560168bSGuenter Roeck 	return hwdev;
829d560168bSGuenter Roeck 
830d560168bSGuenter Roeck error:
831d560168bSGuenter Roeck 	devres_free(ptr);
832d560168bSGuenter Roeck 	return hwdev;
833d560168bSGuenter Roeck }
834d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
835d560168bSGuenter Roeck 
83674188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data)
83774188cbaSGuenter Roeck {
83874188cbaSGuenter Roeck 	struct device **hwdev = res;
83974188cbaSGuenter Roeck 
84074188cbaSGuenter Roeck 	return *hwdev == data;
84174188cbaSGuenter Roeck }
84274188cbaSGuenter Roeck 
84374188cbaSGuenter Roeck /**
84474188cbaSGuenter Roeck  * devm_hwmon_device_unregister - removes a previously registered hwmon device
84574188cbaSGuenter Roeck  *
84674188cbaSGuenter Roeck  * @dev: the parent device of the device to unregister
84774188cbaSGuenter Roeck  */
84874188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev)
84974188cbaSGuenter Roeck {
85074188cbaSGuenter Roeck 	WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
85174188cbaSGuenter Roeck }
85274188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
85374188cbaSGuenter Roeck 
8542958b1ecSJean Delvare static void __init hwmon_pci_quirks(void)
8552958b1ecSJean Delvare {
8562958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI
8572958b1ecSJean Delvare 	struct pci_dev *sb;
8582958b1ecSJean Delvare 	u16 base;
8592958b1ecSJean Delvare 	u8 enable;
8602958b1ecSJean Delvare 
8612958b1ecSJean Delvare 	/* Open access to 0x295-0x296 on MSI MS-7031 */
8622958b1ecSJean Delvare 	sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
863d6dab7ddSJean Delvare 	if (sb) {
864d6dab7ddSJean Delvare 		if (sb->subsystem_vendor == 0x1462 &&	/* MSI */
865d6dab7ddSJean Delvare 		    sb->subsystem_device == 0x0031) {	/* MS-7031 */
8662958b1ecSJean Delvare 			pci_read_config_byte(sb, 0x48, &enable);
8672958b1ecSJean Delvare 			pci_read_config_word(sb, 0x64, &base);
8682958b1ecSJean Delvare 
8692958b1ecSJean Delvare 			if (base == 0 && !(enable & BIT(2))) {
8702958b1ecSJean Delvare 				dev_info(&sb->dev,
8712958b1ecSJean Delvare 					 "Opening wide generic port at 0x295\n");
8722958b1ecSJean Delvare 				pci_write_config_word(sb, 0x64, 0x295);
873d6dab7ddSJean Delvare 				pci_write_config_byte(sb, 0x48,
874d6dab7ddSJean Delvare 						      enable | BIT(2));
8752958b1ecSJean Delvare 			}
8762958b1ecSJean Delvare 		}
877d6dab7ddSJean Delvare 		pci_dev_put(sb);
878d6dab7ddSJean Delvare 	}
8792958b1ecSJean Delvare #endif
8802958b1ecSJean Delvare }
8812958b1ecSJean Delvare 
8821236441fSMark M. Hoffman static int __init hwmon_init(void)
8831236441fSMark M. Hoffman {
884bab2243cSGuenter Roeck 	int err;
885bab2243cSGuenter Roeck 
8862958b1ecSJean Delvare 	hwmon_pci_quirks();
8872958b1ecSJean Delvare 
888bab2243cSGuenter Roeck 	err = class_register(&hwmon_class);
889bab2243cSGuenter Roeck 	if (err) {
890bab2243cSGuenter Roeck 		pr_err("couldn't register hwmon sysfs class\n");
891bab2243cSGuenter Roeck 		return err;
8921236441fSMark M. Hoffman 	}
8931236441fSMark M. Hoffman 	return 0;
8941236441fSMark M. Hoffman }
8951236441fSMark M. Hoffman 
8961236441fSMark M. Hoffman static void __exit hwmon_exit(void)
8971236441fSMark M. Hoffman {
898bab2243cSGuenter Roeck 	class_unregister(&hwmon_class);
8991236441fSMark M. Hoffman }
9001236441fSMark M. Hoffman 
90137f54ee5SDavid Brownell subsys_initcall(hwmon_init);
9021236441fSMark M. Hoffman module_exit(hwmon_exit);
9031236441fSMark M. Hoffman 
9041236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
9051236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
9061236441fSMark M. Hoffman MODULE_LICENSE("GPL");
9071236441fSMark M. Hoffman 
908