xref: /linux/drivers/hwmon/hwmon.c (revision f37353320ee9905ef7aaba4c67eae85496bee950)
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 
2761b8ab2cSNicolin Chen #define CREATE_TRACE_POINTS
2861b8ab2cSNicolin Chen #include <trace/events/hwmon.h>
2961b8ab2cSNicolin Chen 
301236441fSMark M. Hoffman #define HWMON_ID_PREFIX "hwmon"
311236441fSMark M. Hoffman #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
321236441fSMark M. Hoffman 
33bab2243cSGuenter Roeck struct hwmon_device {
34bab2243cSGuenter Roeck 	const char *name;
35bab2243cSGuenter Roeck 	struct device dev;
36d560168bSGuenter Roeck 	const struct hwmon_chip_info *chip;
37d560168bSGuenter Roeck 
38d560168bSGuenter Roeck 	struct attribute_group group;
39d560168bSGuenter Roeck 	const struct attribute_group **groups;
40bab2243cSGuenter Roeck };
41d560168bSGuenter Roeck 
42bab2243cSGuenter Roeck #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
43bab2243cSGuenter Roeck 
443a412d5eSGuenter Roeck #define MAX_SYSFS_ATTR_NAME_LENGTH	32
453a412d5eSGuenter Roeck 
46d560168bSGuenter Roeck struct hwmon_device_attribute {
47d560168bSGuenter Roeck 	struct device_attribute dev_attr;
48d560168bSGuenter Roeck 	const struct hwmon_ops *ops;
49d560168bSGuenter Roeck 	enum hwmon_sensor_types type;
50d560168bSGuenter Roeck 	u32 attr;
51d560168bSGuenter Roeck 	int index;
523a412d5eSGuenter Roeck 	char name[MAX_SYSFS_ATTR_NAME_LENGTH];
53d560168bSGuenter Roeck };
54d560168bSGuenter Roeck 
55d560168bSGuenter Roeck #define to_hwmon_attr(d) \
56d560168bSGuenter Roeck 	container_of(d, struct hwmon_device_attribute, dev_attr)
57d560168bSGuenter Roeck 
58d560168bSGuenter Roeck /*
59d560168bSGuenter Roeck  * Thermal zone information
60d560168bSGuenter Roeck  * In addition to the reference to the hwmon device,
61d560168bSGuenter Roeck  * also provides the sensor index.
62d560168bSGuenter Roeck  */
63d560168bSGuenter Roeck struct hwmon_thermal_data {
64d560168bSGuenter Roeck 	struct hwmon_device *hwdev;	/* Reference to hwmon device */
65d560168bSGuenter Roeck 	int index;			/* sensor index */
66d560168bSGuenter Roeck };
67d560168bSGuenter Roeck 
68bab2243cSGuenter Roeck static ssize_t
692ab0c6c5SJulia Lawall name_show(struct device *dev, struct device_attribute *attr, char *buf)
70bab2243cSGuenter Roeck {
71bab2243cSGuenter Roeck 	return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
72bab2243cSGuenter Roeck }
732ab0c6c5SJulia Lawall static DEVICE_ATTR_RO(name);
74bab2243cSGuenter Roeck 
75bab2243cSGuenter Roeck static struct attribute *hwmon_dev_attrs[] = {
76bab2243cSGuenter Roeck 	&dev_attr_name.attr,
77bab2243cSGuenter Roeck 	NULL
78bab2243cSGuenter Roeck };
79bab2243cSGuenter Roeck 
80bab2243cSGuenter Roeck static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
81bab2243cSGuenter Roeck 					 struct attribute *attr, int n)
82bab2243cSGuenter Roeck {
83bab2243cSGuenter Roeck 	struct device *dev = container_of(kobj, struct device, kobj);
84bab2243cSGuenter Roeck 
85bab2243cSGuenter Roeck 	if (to_hwmon_device(dev)->name == NULL)
86bab2243cSGuenter Roeck 		return 0;
87bab2243cSGuenter Roeck 
88bab2243cSGuenter Roeck 	return attr->mode;
89bab2243cSGuenter Roeck }
90bab2243cSGuenter Roeck 
91524703acSArvind Yadav static const struct attribute_group hwmon_dev_attr_group = {
92bab2243cSGuenter Roeck 	.attrs		= hwmon_dev_attrs,
93bab2243cSGuenter Roeck 	.is_visible	= hwmon_dev_name_is_visible,
94bab2243cSGuenter Roeck };
95bab2243cSGuenter Roeck 
96bab2243cSGuenter Roeck static const struct attribute_group *hwmon_dev_attr_groups[] = {
97bab2243cSGuenter Roeck 	&hwmon_dev_attr_group,
98bab2243cSGuenter Roeck 	NULL
99bab2243cSGuenter Roeck };
100bab2243cSGuenter Roeck 
101bab2243cSGuenter Roeck static void hwmon_dev_release(struct device *dev)
102bab2243cSGuenter Roeck {
103bab2243cSGuenter Roeck 	kfree(to_hwmon_device(dev));
104bab2243cSGuenter Roeck }
105bab2243cSGuenter Roeck 
106bab2243cSGuenter Roeck static struct class hwmon_class = {
107bab2243cSGuenter Roeck 	.name = "hwmon",
108bab2243cSGuenter Roeck 	.owner = THIS_MODULE,
109bab2243cSGuenter Roeck 	.dev_groups = hwmon_dev_attr_groups,
110bab2243cSGuenter Roeck 	.dev_release = hwmon_dev_release,
111bab2243cSGuenter Roeck };
1121236441fSMark M. Hoffman 
1134ca5f468SJonathan Cameron static DEFINE_IDA(hwmon_ida);
1141236441fSMark M. Hoffman 
115d560168bSGuenter Roeck /* Thermal zone handling */
116d560168bSGuenter Roeck 
11786430c1aSGuenter Roeck /*
11886430c1aSGuenter Roeck  * The complex conditional is necessary to avoid a cyclic dependency
11986430c1aSGuenter Roeck  * between hwmon and thermal_sys modules.
12086430c1aSGuenter Roeck  */
121*f3735332SDaniel Lezcano #ifdef CONFIG_THERMAL_OF
122d560168bSGuenter Roeck static int hwmon_thermal_get_temp(void *data, int *temp)
123d560168bSGuenter Roeck {
124d560168bSGuenter Roeck 	struct hwmon_thermal_data *tdata = data;
125d560168bSGuenter Roeck 	struct hwmon_device *hwdev = tdata->hwdev;
126d560168bSGuenter Roeck 	int ret;
127d560168bSGuenter Roeck 	long t;
128d560168bSGuenter Roeck 
129d560168bSGuenter Roeck 	ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
130d560168bSGuenter Roeck 				     tdata->index, &t);
131d560168bSGuenter Roeck 	if (ret < 0)
132d560168bSGuenter Roeck 		return ret;
133d560168bSGuenter Roeck 
134d560168bSGuenter Roeck 	*temp = t;
135d560168bSGuenter Roeck 
136d560168bSGuenter Roeck 	return 0;
137d560168bSGuenter Roeck }
138d560168bSGuenter Roeck 
139c9920650SJulia Lawall static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
140d560168bSGuenter Roeck 	.get_temp = hwmon_thermal_get_temp,
141d560168bSGuenter Roeck };
142d560168bSGuenter Roeck 
143d560168bSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev,
144d560168bSGuenter Roeck 				    struct hwmon_device *hwdev, int index)
145d560168bSGuenter Roeck {
146d560168bSGuenter Roeck 	struct hwmon_thermal_data *tdata;
14747c332deSLinus Walleij 	struct thermal_zone_device *tzd;
148d560168bSGuenter Roeck 
149d560168bSGuenter Roeck 	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
150d560168bSGuenter Roeck 	if (!tdata)
151d560168bSGuenter Roeck 		return -ENOMEM;
152d560168bSGuenter Roeck 
153d560168bSGuenter Roeck 	tdata->hwdev = hwdev;
154d560168bSGuenter Roeck 	tdata->index = index;
155d560168bSGuenter Roeck 
15647c332deSLinus Walleij 	tzd = devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
157d560168bSGuenter Roeck 						   &hwmon_thermal_ops);
15847c332deSLinus Walleij 	/*
15947c332deSLinus Walleij 	 * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV,
16047c332deSLinus Walleij 	 * so ignore that error but forward any other error.
16147c332deSLinus Walleij 	 */
16247c332deSLinus Walleij 	if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV))
16347c332deSLinus Walleij 		return PTR_ERR(tzd);
164d560168bSGuenter Roeck 
165d560168bSGuenter Roeck 	return 0;
166d560168bSGuenter Roeck }
167d560168bSGuenter Roeck #else
168d560168bSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev,
169d560168bSGuenter Roeck 				    struct hwmon_device *hwdev, int index)
170d560168bSGuenter Roeck {
171d560168bSGuenter Roeck 	return 0;
172d560168bSGuenter Roeck }
17386430c1aSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
174d560168bSGuenter Roeck 
17561b8ab2cSNicolin Chen static int hwmon_attr_base(enum hwmon_sensor_types type)
17661b8ab2cSNicolin Chen {
17761b8ab2cSNicolin Chen 	if (type == hwmon_in)
17861b8ab2cSNicolin Chen 		return 0;
17961b8ab2cSNicolin Chen 	return 1;
18061b8ab2cSNicolin Chen }
18161b8ab2cSNicolin Chen 
182d560168bSGuenter Roeck /* sysfs attribute management */
183d560168bSGuenter Roeck 
184d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev,
185d560168bSGuenter Roeck 			       struct device_attribute *devattr, char *buf)
186d560168bSGuenter Roeck {
187d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
188d560168bSGuenter Roeck 	long val;
189d560168bSGuenter Roeck 	int ret;
190d560168bSGuenter Roeck 
191d560168bSGuenter Roeck 	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
192d560168bSGuenter Roeck 			       &val);
193d560168bSGuenter Roeck 	if (ret < 0)
194d560168bSGuenter Roeck 		return ret;
195d560168bSGuenter Roeck 
19661b8ab2cSNicolin Chen 	trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
19761b8ab2cSNicolin Chen 			      hattr->name, val);
19861b8ab2cSNicolin Chen 
199d560168bSGuenter Roeck 	return sprintf(buf, "%ld\n", val);
200d560168bSGuenter Roeck }
201d560168bSGuenter Roeck 
202e159ab5cSGuenter Roeck static ssize_t hwmon_attr_show_string(struct device *dev,
203e159ab5cSGuenter Roeck 				      struct device_attribute *devattr,
204e159ab5cSGuenter Roeck 				      char *buf)
205e159ab5cSGuenter Roeck {
206e159ab5cSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
20761b8ab2cSNicolin Chen 	enum hwmon_sensor_types type = hattr->type;
2085ba6bcbcSJean Delvare 	const char *s;
209e159ab5cSGuenter Roeck 	int ret;
210e159ab5cSGuenter Roeck 
211e159ab5cSGuenter Roeck 	ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
212e159ab5cSGuenter Roeck 				      hattr->index, &s);
213e159ab5cSGuenter Roeck 	if (ret < 0)
214e159ab5cSGuenter Roeck 		return ret;
215e159ab5cSGuenter Roeck 
21661b8ab2cSNicolin Chen 	trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
21761b8ab2cSNicolin Chen 				     hattr->name, s);
21861b8ab2cSNicolin Chen 
219e159ab5cSGuenter Roeck 	return sprintf(buf, "%s\n", s);
220e159ab5cSGuenter Roeck }
221e159ab5cSGuenter Roeck 
222d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev,
223d560168bSGuenter Roeck 				struct device_attribute *devattr,
224d560168bSGuenter Roeck 				const char *buf, size_t count)
225d560168bSGuenter Roeck {
226d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
227d560168bSGuenter Roeck 	long val;
228d560168bSGuenter Roeck 	int ret;
229d560168bSGuenter Roeck 
230d560168bSGuenter Roeck 	ret = kstrtol(buf, 10, &val);
231d560168bSGuenter Roeck 	if (ret < 0)
232d560168bSGuenter Roeck 		return ret;
233d560168bSGuenter Roeck 
234d560168bSGuenter Roeck 	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
235d560168bSGuenter Roeck 				val);
236d560168bSGuenter Roeck 	if (ret < 0)
237d560168bSGuenter Roeck 		return ret;
238d560168bSGuenter Roeck 
23961b8ab2cSNicolin Chen 	trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
24061b8ab2cSNicolin Chen 			       hattr->name, val);
241d560168bSGuenter Roeck 
24261b8ab2cSNicolin Chen 	return count;
243d560168bSGuenter Roeck }
244d560168bSGuenter Roeck 
245e159ab5cSGuenter Roeck static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
246e159ab5cSGuenter Roeck {
247e159ab5cSGuenter Roeck 	return (type == hwmon_temp && attr == hwmon_temp_label) ||
248e159ab5cSGuenter Roeck 	       (type == hwmon_in && attr == hwmon_in_label) ||
249e159ab5cSGuenter Roeck 	       (type == hwmon_curr && attr == hwmon_curr_label) ||
250e159ab5cSGuenter Roeck 	       (type == hwmon_power && attr == hwmon_power_label) ||
251e159ab5cSGuenter Roeck 	       (type == hwmon_energy && attr == hwmon_energy_label) ||
252e159ab5cSGuenter Roeck 	       (type == hwmon_humidity && attr == hwmon_humidity_label) ||
253e159ab5cSGuenter Roeck 	       (type == hwmon_fan && attr == hwmon_fan_label);
254e159ab5cSGuenter Roeck }
255e159ab5cSGuenter Roeck 
256d560168bSGuenter Roeck static struct attribute *hwmon_genattr(struct device *dev,
257d560168bSGuenter Roeck 				       const void *drvdata,
258d560168bSGuenter Roeck 				       enum hwmon_sensor_types type,
259d560168bSGuenter Roeck 				       u32 attr,
260d560168bSGuenter Roeck 				       int index,
261d560168bSGuenter Roeck 				       const char *template,
262d560168bSGuenter Roeck 				       const struct hwmon_ops *ops)
263d560168bSGuenter Roeck {
264d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr;
265d560168bSGuenter Roeck 	struct device_attribute *dattr;
266d560168bSGuenter Roeck 	struct attribute *a;
267d560168bSGuenter Roeck 	umode_t mode;
2683b443defSRasmus Villemoes 	const char *name;
269e159ab5cSGuenter Roeck 	bool is_string = is_string_attr(type, attr);
270d560168bSGuenter Roeck 
271d560168bSGuenter Roeck 	/* The attribute is invisible if there is no template string */
272d560168bSGuenter Roeck 	if (!template)
273d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
274d560168bSGuenter Roeck 
275d560168bSGuenter Roeck 	mode = ops->is_visible(drvdata, type, attr, index);
276d560168bSGuenter Roeck 	if (!mode)
277d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
278d560168bSGuenter Roeck 
2790d87116fSGuenter Roeck 	if ((mode & 0444) && ((is_string && !ops->read_string) ||
280e159ab5cSGuenter Roeck 				 (!is_string && !ops->read)))
281d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
2820d87116fSGuenter Roeck 	if ((mode & 0222) && !ops->write)
283d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
284d560168bSGuenter Roeck 
285d560168bSGuenter Roeck 	hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
286d560168bSGuenter Roeck 	if (!hattr)
287d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
288d560168bSGuenter Roeck 
2893a412d5eSGuenter Roeck 	if (type == hwmon_chip) {
2903b443defSRasmus Villemoes 		name = template;
2913a412d5eSGuenter Roeck 	} else {
2923a412d5eSGuenter Roeck 		scnprintf(hattr->name, sizeof(hattr->name), template,
2933a412d5eSGuenter Roeck 			  index + hwmon_attr_base(type));
2943a412d5eSGuenter Roeck 		name = hattr->name;
2953a412d5eSGuenter Roeck 	}
2963a412d5eSGuenter Roeck 
297d560168bSGuenter Roeck 	hattr->type = type;
298d560168bSGuenter Roeck 	hattr->attr = attr;
299d560168bSGuenter Roeck 	hattr->index = index;
300d560168bSGuenter Roeck 	hattr->ops = ops;
301d560168bSGuenter Roeck 
302d560168bSGuenter Roeck 	dattr = &hattr->dev_attr;
303e159ab5cSGuenter Roeck 	dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
304d560168bSGuenter Roeck 	dattr->store = hwmon_attr_store;
305d560168bSGuenter Roeck 
306d560168bSGuenter Roeck 	a = &dattr->attr;
307d560168bSGuenter Roeck 	sysfs_attr_init(a);
308d560168bSGuenter Roeck 	a->name = name;
309d560168bSGuenter Roeck 	a->mode = mode;
310d560168bSGuenter Roeck 
311d560168bSGuenter Roeck 	return a;
312d560168bSGuenter Roeck }
313d560168bSGuenter Roeck 
314f4d325d5SGuenter Roeck /*
315f4d325d5SGuenter Roeck  * Chip attributes are not attribute templates but actual sysfs attributes.
316f4d325d5SGuenter Roeck  * See hwmon_genattr() for special handling.
317f4d325d5SGuenter Roeck  */
318f4d325d5SGuenter Roeck static const char * const hwmon_chip_attrs[] = {
319d560168bSGuenter Roeck 	[hwmon_chip_temp_reset_history] = "temp_reset_history",
32000d616cfSGuenter Roeck 	[hwmon_chip_in_reset_history] = "in_reset_history",
3219b26947cSGuenter Roeck 	[hwmon_chip_curr_reset_history] = "curr_reset_history",
322b308f5c7SGuenter Roeck 	[hwmon_chip_power_reset_history] = "power_reset_history",
323d560168bSGuenter Roeck 	[hwmon_chip_update_interval] = "update_interval",
324d560168bSGuenter Roeck 	[hwmon_chip_alarms] = "alarms",
325d560168bSGuenter Roeck };
326d560168bSGuenter Roeck 
327d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = {
328d560168bSGuenter Roeck 	[hwmon_temp_input] = "temp%d_input",
329d560168bSGuenter Roeck 	[hwmon_temp_type] = "temp%d_type",
330d560168bSGuenter Roeck 	[hwmon_temp_lcrit] = "temp%d_lcrit",
331d560168bSGuenter Roeck 	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
332d560168bSGuenter Roeck 	[hwmon_temp_min] = "temp%d_min",
333d560168bSGuenter Roeck 	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
334d560168bSGuenter Roeck 	[hwmon_temp_max] = "temp%d_max",
335d560168bSGuenter Roeck 	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
336d560168bSGuenter Roeck 	[hwmon_temp_crit] = "temp%d_crit",
337d560168bSGuenter Roeck 	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
338d560168bSGuenter Roeck 	[hwmon_temp_emergency] = "temp%d_emergency",
339d560168bSGuenter Roeck 	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
340d560168bSGuenter Roeck 	[hwmon_temp_alarm] = "temp%d_alarm",
341d560168bSGuenter Roeck 	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
342d560168bSGuenter Roeck 	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
343d560168bSGuenter Roeck 	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
344d560168bSGuenter Roeck 	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
345d560168bSGuenter Roeck 	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
346d560168bSGuenter Roeck 	[hwmon_temp_fault] = "temp%d_fault",
347d560168bSGuenter Roeck 	[hwmon_temp_offset] = "temp%d_offset",
348d560168bSGuenter Roeck 	[hwmon_temp_label] = "temp%d_label",
349d560168bSGuenter Roeck 	[hwmon_temp_lowest] = "temp%d_lowest",
350d560168bSGuenter Roeck 	[hwmon_temp_highest] = "temp%d_highest",
351d560168bSGuenter Roeck 	[hwmon_temp_reset_history] = "temp%d_reset_history",
352d560168bSGuenter Roeck };
353d560168bSGuenter Roeck 
35400d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = {
35500d616cfSGuenter Roeck 	[hwmon_in_input] = "in%d_input",
35600d616cfSGuenter Roeck 	[hwmon_in_min] = "in%d_min",
35700d616cfSGuenter Roeck 	[hwmon_in_max] = "in%d_max",
35800d616cfSGuenter Roeck 	[hwmon_in_lcrit] = "in%d_lcrit",
35900d616cfSGuenter Roeck 	[hwmon_in_crit] = "in%d_crit",
36000d616cfSGuenter Roeck 	[hwmon_in_average] = "in%d_average",
36100d616cfSGuenter Roeck 	[hwmon_in_lowest] = "in%d_lowest",
36200d616cfSGuenter Roeck 	[hwmon_in_highest] = "in%d_highest",
36300d616cfSGuenter Roeck 	[hwmon_in_reset_history] = "in%d_reset_history",
36400d616cfSGuenter Roeck 	[hwmon_in_label] = "in%d_label",
36500d616cfSGuenter Roeck 	[hwmon_in_alarm] = "in%d_alarm",
36600d616cfSGuenter Roeck 	[hwmon_in_min_alarm] = "in%d_min_alarm",
36700d616cfSGuenter Roeck 	[hwmon_in_max_alarm] = "in%d_max_alarm",
36800d616cfSGuenter Roeck 	[hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
36900d616cfSGuenter Roeck 	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
37068c0d69dSNicolin Chen 	[hwmon_in_enable] = "in%d_enable",
37100d616cfSGuenter Roeck };
37200d616cfSGuenter Roeck 
3739b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = {
3749b26947cSGuenter Roeck 	[hwmon_curr_input] = "curr%d_input",
3759b26947cSGuenter Roeck 	[hwmon_curr_min] = "curr%d_min",
3769b26947cSGuenter Roeck 	[hwmon_curr_max] = "curr%d_max",
3779b26947cSGuenter Roeck 	[hwmon_curr_lcrit] = "curr%d_lcrit",
3789b26947cSGuenter Roeck 	[hwmon_curr_crit] = "curr%d_crit",
3799b26947cSGuenter Roeck 	[hwmon_curr_average] = "curr%d_average",
3809b26947cSGuenter Roeck 	[hwmon_curr_lowest] = "curr%d_lowest",
3819b26947cSGuenter Roeck 	[hwmon_curr_highest] = "curr%d_highest",
3829b26947cSGuenter Roeck 	[hwmon_curr_reset_history] = "curr%d_reset_history",
3839b26947cSGuenter Roeck 	[hwmon_curr_label] = "curr%d_label",
3849b26947cSGuenter Roeck 	[hwmon_curr_alarm] = "curr%d_alarm",
3859b26947cSGuenter Roeck 	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
3869b26947cSGuenter Roeck 	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
3879b26947cSGuenter Roeck 	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
3889b26947cSGuenter Roeck 	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
3899b26947cSGuenter Roeck };
3909b26947cSGuenter Roeck 
391b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = {
392b308f5c7SGuenter Roeck 	[hwmon_power_average] = "power%d_average",
393b308f5c7SGuenter Roeck 	[hwmon_power_average_interval] = "power%d_average_interval",
394b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_max] = "power%d_interval_max",
395b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_min] = "power%d_interval_min",
396b308f5c7SGuenter Roeck 	[hwmon_power_average_highest] = "power%d_average_highest",
397b308f5c7SGuenter Roeck 	[hwmon_power_average_lowest] = "power%d_average_lowest",
398b308f5c7SGuenter Roeck 	[hwmon_power_average_max] = "power%d_average_max",
399b308f5c7SGuenter Roeck 	[hwmon_power_average_min] = "power%d_average_min",
400b308f5c7SGuenter Roeck 	[hwmon_power_input] = "power%d_input",
401b308f5c7SGuenter Roeck 	[hwmon_power_input_highest] = "power%d_input_highest",
402b308f5c7SGuenter Roeck 	[hwmon_power_input_lowest] = "power%d_input_lowest",
403b308f5c7SGuenter Roeck 	[hwmon_power_reset_history] = "power%d_reset_history",
404b308f5c7SGuenter Roeck 	[hwmon_power_accuracy] = "power%d_accuracy",
405b308f5c7SGuenter Roeck 	[hwmon_power_cap] = "power%d_cap",
406b308f5c7SGuenter Roeck 	[hwmon_power_cap_hyst] = "power%d_cap_hyst",
407b308f5c7SGuenter Roeck 	[hwmon_power_cap_max] = "power%d_cap_max",
408b308f5c7SGuenter Roeck 	[hwmon_power_cap_min] = "power%d_cap_min",
409aa7f29b0SAndrew Lunn 	[hwmon_power_min] = "power%d_min",
410b308f5c7SGuenter Roeck 	[hwmon_power_max] = "power%d_max",
411aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit] = "power%d_lcrit",
412b308f5c7SGuenter Roeck 	[hwmon_power_crit] = "power%d_crit",
413b308f5c7SGuenter Roeck 	[hwmon_power_label] = "power%d_label",
414b308f5c7SGuenter Roeck 	[hwmon_power_alarm] = "power%d_alarm",
415b308f5c7SGuenter Roeck 	[hwmon_power_cap_alarm] = "power%d_cap_alarm",
416aa7f29b0SAndrew Lunn 	[hwmon_power_min_alarm] = "power%d_min_alarm",
417b308f5c7SGuenter Roeck 	[hwmon_power_max_alarm] = "power%d_max_alarm",
418aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
419b308f5c7SGuenter Roeck 	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
420b308f5c7SGuenter Roeck };
421b308f5c7SGuenter Roeck 
4226bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = {
4236bfcca44SGuenter Roeck 	[hwmon_energy_input] = "energy%d_input",
4246bfcca44SGuenter Roeck 	[hwmon_energy_label] = "energy%d_label",
4256bfcca44SGuenter Roeck };
4266bfcca44SGuenter Roeck 
4276bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = {
4286bfcca44SGuenter Roeck 	[hwmon_humidity_input] = "humidity%d_input",
4296bfcca44SGuenter Roeck 	[hwmon_humidity_label] = "humidity%d_label",
4306bfcca44SGuenter Roeck 	[hwmon_humidity_min] = "humidity%d_min",
4316bfcca44SGuenter Roeck 	[hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
4326bfcca44SGuenter Roeck 	[hwmon_humidity_max] = "humidity%d_max",
4336bfcca44SGuenter Roeck 	[hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
4346bfcca44SGuenter Roeck 	[hwmon_humidity_alarm] = "humidity%d_alarm",
4356bfcca44SGuenter Roeck 	[hwmon_humidity_fault] = "humidity%d_fault",
4366bfcca44SGuenter Roeck };
4376bfcca44SGuenter Roeck 
4388faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = {
4398faee73fSGuenter Roeck 	[hwmon_fan_input] = "fan%d_input",
4408faee73fSGuenter Roeck 	[hwmon_fan_label] = "fan%d_label",
4418faee73fSGuenter Roeck 	[hwmon_fan_min] = "fan%d_min",
4428faee73fSGuenter Roeck 	[hwmon_fan_max] = "fan%d_max",
4438faee73fSGuenter Roeck 	[hwmon_fan_div] = "fan%d_div",
4448faee73fSGuenter Roeck 	[hwmon_fan_pulses] = "fan%d_pulses",
4458faee73fSGuenter Roeck 	[hwmon_fan_target] = "fan%d_target",
4468faee73fSGuenter Roeck 	[hwmon_fan_alarm] = "fan%d_alarm",
4478faee73fSGuenter Roeck 	[hwmon_fan_min_alarm] = "fan%d_min_alarm",
4488faee73fSGuenter Roeck 	[hwmon_fan_max_alarm] = "fan%d_max_alarm",
4498faee73fSGuenter Roeck 	[hwmon_fan_fault] = "fan%d_fault",
4508faee73fSGuenter Roeck };
4518faee73fSGuenter Roeck 
452f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = {
453f9f7bb3aSGuenter Roeck 	[hwmon_pwm_input] = "pwm%d",
454f9f7bb3aSGuenter Roeck 	[hwmon_pwm_enable] = "pwm%d_enable",
455f9f7bb3aSGuenter Roeck 	[hwmon_pwm_mode] = "pwm%d_mode",
456f9f7bb3aSGuenter Roeck 	[hwmon_pwm_freq] = "pwm%d_freq",
457f9f7bb3aSGuenter Roeck };
458f9f7bb3aSGuenter Roeck 
459d560168bSGuenter Roeck static const char * const *__templates[] = {
460f4d325d5SGuenter Roeck 	[hwmon_chip] = hwmon_chip_attrs,
461d560168bSGuenter Roeck 	[hwmon_temp] = hwmon_temp_attr_templates,
46200d616cfSGuenter Roeck 	[hwmon_in] = hwmon_in_attr_templates,
4639b26947cSGuenter Roeck 	[hwmon_curr] = hwmon_curr_attr_templates,
464b308f5c7SGuenter Roeck 	[hwmon_power] = hwmon_power_attr_templates,
4656bfcca44SGuenter Roeck 	[hwmon_energy] = hwmon_energy_attr_templates,
4666bfcca44SGuenter Roeck 	[hwmon_humidity] = hwmon_humidity_attr_templates,
4678faee73fSGuenter Roeck 	[hwmon_fan] = hwmon_fan_attr_templates,
468f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = hwmon_pwm_attr_templates,
469d560168bSGuenter Roeck };
470d560168bSGuenter Roeck 
471d560168bSGuenter Roeck static const int __templates_size[] = {
472f4d325d5SGuenter Roeck 	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
473d560168bSGuenter Roeck 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
47400d616cfSGuenter Roeck 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
4759b26947cSGuenter Roeck 	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
476b308f5c7SGuenter Roeck 	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
4776bfcca44SGuenter Roeck 	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
4786bfcca44SGuenter Roeck 	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
4798faee73fSGuenter Roeck 	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
480f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
481d560168bSGuenter Roeck };
482d560168bSGuenter Roeck 
483d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
484d560168bSGuenter Roeck {
485d560168bSGuenter Roeck 	int i, n;
486d560168bSGuenter Roeck 
487d560168bSGuenter Roeck 	for (i = n = 0; info->config[i]; i++)
488d560168bSGuenter Roeck 		n += hweight32(info->config[i]);
489d560168bSGuenter Roeck 
490d560168bSGuenter Roeck 	return n;
491d560168bSGuenter Roeck }
492d560168bSGuenter Roeck 
493d560168bSGuenter Roeck static int hwmon_genattrs(struct device *dev,
494d560168bSGuenter Roeck 			  const void *drvdata,
495d560168bSGuenter Roeck 			  struct attribute **attrs,
496d560168bSGuenter Roeck 			  const struct hwmon_ops *ops,
497d560168bSGuenter Roeck 			  const struct hwmon_channel_info *info)
498d560168bSGuenter Roeck {
499d560168bSGuenter Roeck 	const char * const *templates;
500d560168bSGuenter Roeck 	int template_size;
501d560168bSGuenter Roeck 	int i, aindex = 0;
502d560168bSGuenter Roeck 
503d560168bSGuenter Roeck 	if (info->type >= ARRAY_SIZE(__templates))
504d560168bSGuenter Roeck 		return -EINVAL;
505d560168bSGuenter Roeck 
506d560168bSGuenter Roeck 	templates = __templates[info->type];
507d560168bSGuenter Roeck 	template_size = __templates_size[info->type];
508d560168bSGuenter Roeck 
509d560168bSGuenter Roeck 	for (i = 0; info->config[i]; i++) {
510d560168bSGuenter Roeck 		u32 attr_mask = info->config[i];
511d560168bSGuenter Roeck 		u32 attr;
512d560168bSGuenter Roeck 
513d560168bSGuenter Roeck 		while (attr_mask) {
514d560168bSGuenter Roeck 			struct attribute *a;
515d560168bSGuenter Roeck 
516d560168bSGuenter Roeck 			attr = __ffs(attr_mask);
517d560168bSGuenter Roeck 			attr_mask &= ~BIT(attr);
518d560168bSGuenter Roeck 			if (attr >= template_size)
519d560168bSGuenter Roeck 				return -EINVAL;
520d560168bSGuenter Roeck 			a = hwmon_genattr(dev, drvdata, info->type, attr, i,
521d560168bSGuenter Roeck 					  templates[attr], ops);
522d560168bSGuenter Roeck 			if (IS_ERR(a)) {
523d560168bSGuenter Roeck 				if (PTR_ERR(a) != -ENOENT)
524d560168bSGuenter Roeck 					return PTR_ERR(a);
525d560168bSGuenter Roeck 				continue;
526d560168bSGuenter Roeck 			}
527d560168bSGuenter Roeck 			attrs[aindex++] = a;
528d560168bSGuenter Roeck 		}
529d560168bSGuenter Roeck 	}
530d560168bSGuenter Roeck 	return aindex;
531d560168bSGuenter Roeck }
532d560168bSGuenter Roeck 
533d560168bSGuenter Roeck static struct attribute **
534d560168bSGuenter Roeck __hwmon_create_attrs(struct device *dev, const void *drvdata,
535d560168bSGuenter Roeck 		     const struct hwmon_chip_info *chip)
536d560168bSGuenter Roeck {
537d560168bSGuenter Roeck 	int ret, i, aindex = 0, nattrs = 0;
538d560168bSGuenter Roeck 	struct attribute **attrs;
539d560168bSGuenter Roeck 
540d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++)
541d560168bSGuenter Roeck 		nattrs += hwmon_num_channel_attrs(chip->info[i]);
542d560168bSGuenter Roeck 
543d560168bSGuenter Roeck 	if (nattrs == 0)
544d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
545d560168bSGuenter Roeck 
546d560168bSGuenter Roeck 	attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
547d560168bSGuenter Roeck 	if (!attrs)
548d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
549d560168bSGuenter Roeck 
550d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++) {
551d560168bSGuenter Roeck 		ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
552d560168bSGuenter Roeck 				     chip->info[i]);
553d560168bSGuenter Roeck 		if (ret < 0)
554d560168bSGuenter Roeck 			return ERR_PTR(ret);
555d560168bSGuenter Roeck 		aindex += ret;
556d560168bSGuenter Roeck 	}
557d560168bSGuenter Roeck 
558d560168bSGuenter Roeck 	return attrs;
559d560168bSGuenter Roeck }
560d560168bSGuenter Roeck 
561d560168bSGuenter Roeck static struct device *
562d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
563d560168bSGuenter Roeck 			const struct hwmon_chip_info *chip,
564d560168bSGuenter Roeck 			const struct attribute_group **groups)
565d560168bSGuenter Roeck {
566d560168bSGuenter Roeck 	struct hwmon_device *hwdev;
567d560168bSGuenter Roeck 	struct device *hdev;
568d560168bSGuenter Roeck 	int i, j, err, id;
569d560168bSGuenter Roeck 
57074d3b641SGuenter Roeck 	/* Complain about invalid characters in hwmon name attribute */
571d560168bSGuenter Roeck 	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
57274d3b641SGuenter Roeck 		dev_warn(dev,
57374d3b641SGuenter Roeck 			 "hwmon: '%s' is not a valid name attribute, please fix\n",
57474d3b641SGuenter Roeck 			 name);
575d560168bSGuenter Roeck 
576d560168bSGuenter Roeck 	id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
577d560168bSGuenter Roeck 	if (id < 0)
578d560168bSGuenter Roeck 		return ERR_PTR(id);
579d560168bSGuenter Roeck 
580d560168bSGuenter Roeck 	hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
581d560168bSGuenter Roeck 	if (hwdev == NULL) {
582d560168bSGuenter Roeck 		err = -ENOMEM;
583d560168bSGuenter Roeck 		goto ida_remove;
584d560168bSGuenter Roeck 	}
585d560168bSGuenter Roeck 
586d560168bSGuenter Roeck 	hdev = &hwdev->dev;
587d560168bSGuenter Roeck 
588239552f4SGuenter Roeck 	if (chip) {
589d560168bSGuenter Roeck 		struct attribute **attrs;
590b2a4cc3aSGuenter Roeck 		int ngroups = 2; /* terminating NULL plus &hwdev->groups */
591d560168bSGuenter Roeck 
592d560168bSGuenter Roeck 		if (groups)
593d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
594d560168bSGuenter Roeck 				ngroups++;
595d560168bSGuenter Roeck 
596d560168bSGuenter Roeck 		hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
597d560168bSGuenter Roeck 					     GFP_KERNEL);
59838d8ed65SColin Ian King 		if (!hwdev->groups) {
59938d8ed65SColin Ian King 			err = -ENOMEM;
60038d8ed65SColin Ian King 			goto free_hwmon;
60138d8ed65SColin Ian King 		}
602d560168bSGuenter Roeck 
603d560168bSGuenter Roeck 		attrs = __hwmon_create_attrs(dev, drvdata, chip);
604d560168bSGuenter Roeck 		if (IS_ERR(attrs)) {
605d560168bSGuenter Roeck 			err = PTR_ERR(attrs);
606d560168bSGuenter Roeck 			goto free_hwmon;
607d560168bSGuenter Roeck 		}
608d560168bSGuenter Roeck 
609d560168bSGuenter Roeck 		hwdev->group.attrs = attrs;
610d560168bSGuenter Roeck 		ngroups = 0;
611d560168bSGuenter Roeck 		hwdev->groups[ngroups++] = &hwdev->group;
612d560168bSGuenter Roeck 
613d560168bSGuenter Roeck 		if (groups) {
614d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
615d560168bSGuenter Roeck 				hwdev->groups[ngroups++] = groups[i];
616d560168bSGuenter Roeck 		}
617d560168bSGuenter Roeck 
618d560168bSGuenter Roeck 		hdev->groups = hwdev->groups;
619d560168bSGuenter Roeck 	} else {
620d560168bSGuenter Roeck 		hdev->groups = groups;
621d560168bSGuenter Roeck 	}
622d560168bSGuenter Roeck 
623d560168bSGuenter Roeck 	hwdev->name = name;
624d560168bSGuenter Roeck 	hdev->class = &hwmon_class;
625d560168bSGuenter Roeck 	hdev->parent = dev;
626d560168bSGuenter Roeck 	hdev->of_node = dev ? dev->of_node : NULL;
627d560168bSGuenter Roeck 	hwdev->chip = chip;
628d560168bSGuenter Roeck 	dev_set_drvdata(hdev, drvdata);
629d560168bSGuenter Roeck 	dev_set_name(hdev, HWMON_ID_FORMAT, id);
630d560168bSGuenter Roeck 	err = device_register(hdev);
631d560168bSGuenter Roeck 	if (err)
632d560168bSGuenter Roeck 		goto free_hwmon;
633d560168bSGuenter Roeck 
634319fe159SGuenter Roeck 	if (dev && chip && chip->ops->read &&
635d560168bSGuenter Roeck 	    chip->info[0]->type == hwmon_chip &&
636d560168bSGuenter Roeck 	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
637d560168bSGuenter Roeck 		const struct hwmon_channel_info **info = chip->info;
638d560168bSGuenter Roeck 
639d560168bSGuenter Roeck 		for (i = 1; info[i]; i++) {
640d560168bSGuenter Roeck 			if (info[i]->type != hwmon_temp)
641d560168bSGuenter Roeck 				continue;
642d560168bSGuenter Roeck 
643d560168bSGuenter Roeck 			for (j = 0; info[i]->config[j]; j++) {
644d560168bSGuenter Roeck 				if (!chip->ops->is_visible(drvdata, hwmon_temp,
645d560168bSGuenter Roeck 							   hwmon_temp_input, j))
646d560168bSGuenter Roeck 					continue;
64747c332deSLinus Walleij 				if (info[i]->config[j] & HWMON_T_INPUT) {
64847c332deSLinus Walleij 					err = hwmon_thermal_add_sensor(dev,
64947c332deSLinus Walleij 								hwdev, j);
65074e35127SDmitry Osipenko 					if (err) {
65174e35127SDmitry Osipenko 						device_unregister(hdev);
65274e35127SDmitry Osipenko 						goto ida_remove;
65374e35127SDmitry Osipenko 					}
65447c332deSLinus Walleij 				}
655d560168bSGuenter Roeck 			}
656d560168bSGuenter Roeck 		}
657d560168bSGuenter Roeck 	}
658d560168bSGuenter Roeck 
659d560168bSGuenter Roeck 	return hdev;
660d560168bSGuenter Roeck 
661d560168bSGuenter Roeck free_hwmon:
662d560168bSGuenter Roeck 	kfree(hwdev);
663d560168bSGuenter Roeck ida_remove:
664d560168bSGuenter Roeck 	ida_simple_remove(&hwmon_ida, id);
665d560168bSGuenter Roeck 	return ERR_PTR(err);
666d560168bSGuenter Roeck }
667d560168bSGuenter Roeck 
6681236441fSMark M. Hoffman /**
669bab2243cSGuenter Roeck  * hwmon_device_register_with_groups - register w/ hwmon
670bab2243cSGuenter Roeck  * @dev: the parent device
671bab2243cSGuenter Roeck  * @name: hwmon name attribute
672bab2243cSGuenter Roeck  * @drvdata: driver data to attach to created device
673bab2243cSGuenter Roeck  * @groups: List of attribute groups to create
674bab2243cSGuenter Roeck  *
675bab2243cSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
676bab2243cSGuenter Roeck  * longer needed.
677bab2243cSGuenter Roeck  *
678bab2243cSGuenter Roeck  * Returns the pointer to the new device.
679bab2243cSGuenter Roeck  */
680bab2243cSGuenter Roeck struct device *
681bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name,
682bab2243cSGuenter Roeck 				  void *drvdata,
683bab2243cSGuenter Roeck 				  const struct attribute_group **groups)
684bab2243cSGuenter Roeck {
6858353863aSGuenter Roeck 	if (!name)
6868353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
6878353863aSGuenter Roeck 
688d560168bSGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
689bab2243cSGuenter Roeck }
690bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
691bab2243cSGuenter Roeck 
692bab2243cSGuenter Roeck /**
693d560168bSGuenter Roeck  * hwmon_device_register_with_info - register w/ hwmon
694d560168bSGuenter Roeck  * @dev: the parent device
695d560168bSGuenter Roeck  * @name: hwmon name attribute
696d560168bSGuenter Roeck  * @drvdata: driver data to attach to created device
6973870945aSGuenter Roeck  * @chip: pointer to hwmon chip information
698848ba0a2SGuenter Roeck  * @extra_groups: pointer to list of additional non-standard attribute groups
699d560168bSGuenter Roeck  *
700d560168bSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
701d560168bSGuenter Roeck  * longer needed.
702d560168bSGuenter Roeck  *
703d560168bSGuenter Roeck  * Returns the pointer to the new device.
704d560168bSGuenter Roeck  */
705d560168bSGuenter Roeck struct device *
706d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name,
707d560168bSGuenter Roeck 				void *drvdata,
708d560168bSGuenter Roeck 				const struct hwmon_chip_info *chip,
709848ba0a2SGuenter Roeck 				const struct attribute_group **extra_groups)
710d560168bSGuenter Roeck {
7118353863aSGuenter Roeck 	if (!name)
7128353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
7138353863aSGuenter Roeck 
714239552f4SGuenter Roeck 	if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
715d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
716d560168bSGuenter Roeck 
71759df4f4eSLucas Magasweran 	if (chip && !dev)
71859df4f4eSLucas Magasweran 		return ERR_PTR(-EINVAL);
71959df4f4eSLucas Magasweran 
720848ba0a2SGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
721d560168bSGuenter Roeck }
722d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
723d560168bSGuenter Roeck 
724d560168bSGuenter Roeck /**
7251beeffe4STony Jones  * hwmon_device_register - register w/ hwmon
7261236441fSMark M. Hoffman  * @dev: the device to register
7271236441fSMark M. Hoffman  *
7281beeffe4STony Jones  * hwmon_device_unregister() must be called when the device is no
7291236441fSMark M. Hoffman  * longer needed.
7301236441fSMark M. Hoffman  *
7311beeffe4STony Jones  * Returns the pointer to the new device.
7321236441fSMark M. Hoffman  */
7331beeffe4STony Jones struct device *hwmon_device_register(struct device *dev)
7341236441fSMark M. Hoffman {
735af1bd36cSGuenter Roeck 	dev_warn(dev,
736af1bd36cSGuenter Roeck 		 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
737af1bd36cSGuenter Roeck 
7388353863aSGuenter Roeck 	return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
7391236441fSMark M. Hoffman }
740839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register);
7411236441fSMark M. Hoffman 
7421236441fSMark M. Hoffman /**
7431236441fSMark M. Hoffman  * hwmon_device_unregister - removes the previously registered class device
7441236441fSMark M. Hoffman  *
7451beeffe4STony Jones  * @dev: the class device to destroy
7461236441fSMark M. Hoffman  */
7471beeffe4STony Jones void hwmon_device_unregister(struct device *dev)
7481236441fSMark M. Hoffman {
7491236441fSMark M. Hoffman 	int id;
7501236441fSMark M. Hoffman 
751739cf3a2SKay Sievers 	if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
7521beeffe4STony Jones 		device_unregister(dev);
7534ca5f468SJonathan Cameron 		ida_simple_remove(&hwmon_ida, id);
7541236441fSMark M. Hoffman 	} else
7551beeffe4STony Jones 		dev_dbg(dev->parent,
7561236441fSMark M. Hoffman 			"hwmon_device_unregister() failed: bad class ID!\n");
7571236441fSMark M. Hoffman }
758839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister);
7591236441fSMark M. Hoffman 
76074188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res)
76174188cbaSGuenter Roeck {
76274188cbaSGuenter Roeck 	struct device *hwdev = *(struct device **)res;
76374188cbaSGuenter Roeck 
76474188cbaSGuenter Roeck 	hwmon_device_unregister(hwdev);
76574188cbaSGuenter Roeck }
76674188cbaSGuenter Roeck 
76774188cbaSGuenter Roeck /**
76874188cbaSGuenter Roeck  * devm_hwmon_device_register_with_groups - register w/ hwmon
76974188cbaSGuenter Roeck  * @dev: the parent device
77074188cbaSGuenter Roeck  * @name: hwmon name attribute
77174188cbaSGuenter Roeck  * @drvdata: driver data to attach to created device
77274188cbaSGuenter Roeck  * @groups: List of attribute groups to create
77374188cbaSGuenter Roeck  *
77474188cbaSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
77574188cbaSGuenter Roeck  * unregistered with the parent device.
77674188cbaSGuenter Roeck  */
77774188cbaSGuenter Roeck struct device *
77874188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
77974188cbaSGuenter Roeck 				       void *drvdata,
78074188cbaSGuenter Roeck 				       const struct attribute_group **groups)
78174188cbaSGuenter Roeck {
78274188cbaSGuenter Roeck 	struct device **ptr, *hwdev;
78374188cbaSGuenter Roeck 
78474188cbaSGuenter Roeck 	if (!dev)
78574188cbaSGuenter Roeck 		return ERR_PTR(-EINVAL);
78674188cbaSGuenter Roeck 
78774188cbaSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
78874188cbaSGuenter Roeck 	if (!ptr)
78974188cbaSGuenter Roeck 		return ERR_PTR(-ENOMEM);
79074188cbaSGuenter Roeck 
79174188cbaSGuenter Roeck 	hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
79274188cbaSGuenter Roeck 	if (IS_ERR(hwdev))
79374188cbaSGuenter Roeck 		goto error;
79474188cbaSGuenter Roeck 
79574188cbaSGuenter Roeck 	*ptr = hwdev;
79674188cbaSGuenter Roeck 	devres_add(dev, ptr);
79774188cbaSGuenter Roeck 	return hwdev;
79874188cbaSGuenter Roeck 
79974188cbaSGuenter Roeck error:
80074188cbaSGuenter Roeck 	devres_free(ptr);
80174188cbaSGuenter Roeck 	return hwdev;
80274188cbaSGuenter Roeck }
80374188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
80474188cbaSGuenter Roeck 
805d560168bSGuenter Roeck /**
806d560168bSGuenter Roeck  * devm_hwmon_device_register_with_info - register w/ hwmon
807d560168bSGuenter Roeck  * @dev:	the parent device
808d560168bSGuenter Roeck  * @name:	hwmon name attribute
809d560168bSGuenter Roeck  * @drvdata:	driver data to attach to created device
8103870945aSGuenter Roeck  * @chip:	pointer to hwmon chip information
8113870945aSGuenter Roeck  * @groups:	pointer to list of driver specific attribute groups
812d560168bSGuenter Roeck  *
813d560168bSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
814d560168bSGuenter Roeck  * unregistered with the parent device.
815d560168bSGuenter Roeck  */
816d560168bSGuenter Roeck struct device *
817d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name,
818d560168bSGuenter Roeck 				     void *drvdata,
819d560168bSGuenter Roeck 				     const struct hwmon_chip_info *chip,
820d560168bSGuenter Roeck 				     const struct attribute_group **groups)
821d560168bSGuenter Roeck {
822d560168bSGuenter Roeck 	struct device **ptr, *hwdev;
823d560168bSGuenter Roeck 
824d560168bSGuenter Roeck 	if (!dev)
825d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
826d560168bSGuenter Roeck 
827d560168bSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
828d560168bSGuenter Roeck 	if (!ptr)
829d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
830d560168bSGuenter Roeck 
831d560168bSGuenter Roeck 	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
832d560168bSGuenter Roeck 						groups);
833d560168bSGuenter Roeck 	if (IS_ERR(hwdev))
834d560168bSGuenter Roeck 		goto error;
835d560168bSGuenter Roeck 
836d560168bSGuenter Roeck 	*ptr = hwdev;
837d560168bSGuenter Roeck 	devres_add(dev, ptr);
838d560168bSGuenter Roeck 
839d560168bSGuenter Roeck 	return hwdev;
840d560168bSGuenter Roeck 
841d560168bSGuenter Roeck error:
842d560168bSGuenter Roeck 	devres_free(ptr);
843d560168bSGuenter Roeck 	return hwdev;
844d560168bSGuenter Roeck }
845d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
846d560168bSGuenter Roeck 
84774188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data)
84874188cbaSGuenter Roeck {
84974188cbaSGuenter Roeck 	struct device **hwdev = res;
85074188cbaSGuenter Roeck 
85174188cbaSGuenter Roeck 	return *hwdev == data;
85274188cbaSGuenter Roeck }
85374188cbaSGuenter Roeck 
85474188cbaSGuenter Roeck /**
85574188cbaSGuenter Roeck  * devm_hwmon_device_unregister - removes a previously registered hwmon device
85674188cbaSGuenter Roeck  *
85774188cbaSGuenter Roeck  * @dev: the parent device of the device to unregister
85874188cbaSGuenter Roeck  */
85974188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev)
86074188cbaSGuenter Roeck {
86174188cbaSGuenter Roeck 	WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
86274188cbaSGuenter Roeck }
86374188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
86474188cbaSGuenter Roeck 
8652958b1ecSJean Delvare static void __init hwmon_pci_quirks(void)
8662958b1ecSJean Delvare {
8672958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI
8682958b1ecSJean Delvare 	struct pci_dev *sb;
8692958b1ecSJean Delvare 	u16 base;
8702958b1ecSJean Delvare 	u8 enable;
8712958b1ecSJean Delvare 
8722958b1ecSJean Delvare 	/* Open access to 0x295-0x296 on MSI MS-7031 */
8732958b1ecSJean Delvare 	sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
874d6dab7ddSJean Delvare 	if (sb) {
875d6dab7ddSJean Delvare 		if (sb->subsystem_vendor == 0x1462 &&	/* MSI */
876d6dab7ddSJean Delvare 		    sb->subsystem_device == 0x0031) {	/* MS-7031 */
8772958b1ecSJean Delvare 			pci_read_config_byte(sb, 0x48, &enable);
8782958b1ecSJean Delvare 			pci_read_config_word(sb, 0x64, &base);
8792958b1ecSJean Delvare 
8802958b1ecSJean Delvare 			if (base == 0 && !(enable & BIT(2))) {
8812958b1ecSJean Delvare 				dev_info(&sb->dev,
8822958b1ecSJean Delvare 					 "Opening wide generic port at 0x295\n");
8832958b1ecSJean Delvare 				pci_write_config_word(sb, 0x64, 0x295);
884d6dab7ddSJean Delvare 				pci_write_config_byte(sb, 0x48,
885d6dab7ddSJean Delvare 						      enable | BIT(2));
8862958b1ecSJean Delvare 			}
8872958b1ecSJean Delvare 		}
888d6dab7ddSJean Delvare 		pci_dev_put(sb);
889d6dab7ddSJean Delvare 	}
8902958b1ecSJean Delvare #endif
8912958b1ecSJean Delvare }
8922958b1ecSJean Delvare 
8931236441fSMark M. Hoffman static int __init hwmon_init(void)
8941236441fSMark M. Hoffman {
895bab2243cSGuenter Roeck 	int err;
896bab2243cSGuenter Roeck 
8972958b1ecSJean Delvare 	hwmon_pci_quirks();
8982958b1ecSJean Delvare 
899bab2243cSGuenter Roeck 	err = class_register(&hwmon_class);
900bab2243cSGuenter Roeck 	if (err) {
901bab2243cSGuenter Roeck 		pr_err("couldn't register hwmon sysfs class\n");
902bab2243cSGuenter Roeck 		return err;
9031236441fSMark M. Hoffman 	}
9041236441fSMark M. Hoffman 	return 0;
9051236441fSMark M. Hoffman }
9061236441fSMark M. Hoffman 
9071236441fSMark M. Hoffman static void __exit hwmon_exit(void)
9081236441fSMark M. Hoffman {
909bab2243cSGuenter Roeck 	class_unregister(&hwmon_class);
9101236441fSMark M. Hoffman }
9111236441fSMark M. Hoffman 
91237f54ee5SDavid Brownell subsys_initcall(hwmon_init);
9131236441fSMark M. Hoffman module_exit(hwmon_exit);
9141236441fSMark M. Hoffman 
9151236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
9161236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
9171236441fSMark M. Hoffman MODULE_LICENSE("GPL");
9181236441fSMark M. Hoffman 
919