xref: /linux/drivers/hwmon/hwmon.c (revision 1597b374af22266266e1e20612208c4b11359ad4)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21236441fSMark M. Hoffman /*
35ed04880SGuenter Roeck  * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
45ed04880SGuenter Roeck  *
55ed04880SGuenter Roeck  * This file defines the sysfs class "hwmon", for use by sensors drivers.
65ed04880SGuenter Roeck  *
75ed04880SGuenter Roeck  * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
81236441fSMark M. Hoffman  */
91236441fSMark M. Hoffman 
10c95df1aeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11c95df1aeSJoe Perches 
12d560168bSGuenter Roeck #include <linux/bitops.h>
131236441fSMark M. Hoffman #include <linux/device.h>
141236441fSMark M. Hoffman #include <linux/err.h>
158c65b4a6STim Schmielau #include <linux/gfp.h>
16c9ebbe6fSGuenter Roeck #include <linux/hwmon.h>
17c9ebbe6fSGuenter Roeck #include <linux/idr.h>
18*1597b374SGuenter Roeck #include <linux/list.h>
19c9ebbe6fSGuenter Roeck #include <linux/module.h>
202958b1ecSJean Delvare #include <linux/pci.h>
21c9ebbe6fSGuenter Roeck #include <linux/slab.h>
22648cd48cSGuenter Roeck #include <linux/string.h>
23d560168bSGuenter Roeck #include <linux/thermal.h>
241236441fSMark M. Hoffman 
2561b8ab2cSNicolin Chen #define CREATE_TRACE_POINTS
2661b8ab2cSNicolin Chen #include <trace/events/hwmon.h>
2761b8ab2cSNicolin Chen 
281236441fSMark M. Hoffman #define HWMON_ID_PREFIX "hwmon"
291236441fSMark M. Hoffman #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
301236441fSMark M. Hoffman 
31bab2243cSGuenter Roeck struct hwmon_device {
32bab2243cSGuenter Roeck 	const char *name;
33bab2243cSGuenter Roeck 	struct device dev;
34d560168bSGuenter Roeck 	const struct hwmon_chip_info *chip;
35*1597b374SGuenter Roeck 	struct list_head tzdata;
36d560168bSGuenter Roeck 	struct attribute_group group;
37d560168bSGuenter Roeck 	const struct attribute_group **groups;
38bab2243cSGuenter Roeck };
39d560168bSGuenter Roeck 
40bab2243cSGuenter Roeck #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
41bab2243cSGuenter Roeck 
423a412d5eSGuenter Roeck #define MAX_SYSFS_ATTR_NAME_LENGTH	32
433a412d5eSGuenter Roeck 
44d560168bSGuenter Roeck struct hwmon_device_attribute {
45d560168bSGuenter Roeck 	struct device_attribute dev_attr;
46d560168bSGuenter Roeck 	const struct hwmon_ops *ops;
47d560168bSGuenter Roeck 	enum hwmon_sensor_types type;
48d560168bSGuenter Roeck 	u32 attr;
49d560168bSGuenter Roeck 	int index;
503a412d5eSGuenter Roeck 	char name[MAX_SYSFS_ATTR_NAME_LENGTH];
51d560168bSGuenter Roeck };
52d560168bSGuenter Roeck 
53d560168bSGuenter Roeck #define to_hwmon_attr(d) \
54d560168bSGuenter Roeck 	container_of(d, struct hwmon_device_attribute, dev_attr)
553bf8bdcfSGuenter Roeck #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
56d560168bSGuenter Roeck 
57d560168bSGuenter Roeck /*
58d560168bSGuenter Roeck  * Thermal zone information
59d560168bSGuenter Roeck  */
60d560168bSGuenter Roeck struct hwmon_thermal_data {
61*1597b374SGuenter Roeck 	struct list_head node;		/* hwmon tzdata list entry */
623bf8bdcfSGuenter Roeck 	struct device *dev;		/* Reference to hwmon device */
63d560168bSGuenter Roeck 	int index;			/* sensor index */
64*1597b374SGuenter Roeck 	struct thermal_zone_device *tzd;/* thermal zone device */
65d560168bSGuenter Roeck };
66d560168bSGuenter Roeck 
67bab2243cSGuenter Roeck static ssize_t
682ab0c6c5SJulia Lawall name_show(struct device *dev, struct device_attribute *attr, char *buf)
69bab2243cSGuenter Roeck {
70bab2243cSGuenter Roeck 	return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
71bab2243cSGuenter Roeck }
722ab0c6c5SJulia Lawall static DEVICE_ATTR_RO(name);
73bab2243cSGuenter Roeck 
74bab2243cSGuenter Roeck static struct attribute *hwmon_dev_attrs[] = {
75bab2243cSGuenter Roeck 	&dev_attr_name.attr,
76bab2243cSGuenter Roeck 	NULL
77bab2243cSGuenter Roeck };
78bab2243cSGuenter Roeck 
79bab2243cSGuenter Roeck static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
80bab2243cSGuenter Roeck 					 struct attribute *attr, int n)
81bab2243cSGuenter Roeck {
82bab2243cSGuenter Roeck 	struct device *dev = container_of(kobj, struct device, kobj);
83bab2243cSGuenter Roeck 
84bab2243cSGuenter Roeck 	if (to_hwmon_device(dev)->name == NULL)
85bab2243cSGuenter Roeck 		return 0;
86bab2243cSGuenter Roeck 
87bab2243cSGuenter Roeck 	return attr->mode;
88bab2243cSGuenter Roeck }
89bab2243cSGuenter Roeck 
90524703acSArvind Yadav static const struct attribute_group hwmon_dev_attr_group = {
91bab2243cSGuenter Roeck 	.attrs		= hwmon_dev_attrs,
92bab2243cSGuenter Roeck 	.is_visible	= hwmon_dev_name_is_visible,
93bab2243cSGuenter Roeck };
94bab2243cSGuenter Roeck 
95bab2243cSGuenter Roeck static const struct attribute_group *hwmon_dev_attr_groups[] = {
96bab2243cSGuenter Roeck 	&hwmon_dev_attr_group,
97bab2243cSGuenter Roeck 	NULL
98bab2243cSGuenter Roeck };
99bab2243cSGuenter Roeck 
1003bf8bdcfSGuenter Roeck static void hwmon_free_attrs(struct attribute **attrs)
1013bf8bdcfSGuenter Roeck {
1023bf8bdcfSGuenter Roeck 	int i;
1033bf8bdcfSGuenter Roeck 
1043bf8bdcfSGuenter Roeck 	for (i = 0; attrs[i]; i++) {
1053bf8bdcfSGuenter Roeck 		struct device_attribute *dattr = to_dev_attr(attrs[i]);
1063bf8bdcfSGuenter Roeck 		struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr);
1073bf8bdcfSGuenter Roeck 
1083bf8bdcfSGuenter Roeck 		kfree(hattr);
1093bf8bdcfSGuenter Roeck 	}
1103bf8bdcfSGuenter Roeck 	kfree(attrs);
1113bf8bdcfSGuenter Roeck }
1123bf8bdcfSGuenter Roeck 
113bab2243cSGuenter Roeck static void hwmon_dev_release(struct device *dev)
114bab2243cSGuenter Roeck {
1153bf8bdcfSGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(dev);
1163bf8bdcfSGuenter Roeck 
1173bf8bdcfSGuenter Roeck 	if (hwdev->group.attrs)
1183bf8bdcfSGuenter Roeck 		hwmon_free_attrs(hwdev->group.attrs);
1193bf8bdcfSGuenter Roeck 	kfree(hwdev->groups);
1203bf8bdcfSGuenter Roeck 	kfree(hwdev);
121bab2243cSGuenter Roeck }
122bab2243cSGuenter Roeck 
123bab2243cSGuenter Roeck static struct class hwmon_class = {
124bab2243cSGuenter Roeck 	.name = "hwmon",
125bab2243cSGuenter Roeck 	.owner = THIS_MODULE,
126bab2243cSGuenter Roeck 	.dev_groups = hwmon_dev_attr_groups,
127bab2243cSGuenter Roeck 	.dev_release = hwmon_dev_release,
128bab2243cSGuenter Roeck };
1291236441fSMark M. Hoffman 
1304ca5f468SJonathan Cameron static DEFINE_IDA(hwmon_ida);
1311236441fSMark M. Hoffman 
132d560168bSGuenter Roeck /* Thermal zone handling */
133d560168bSGuenter Roeck 
13486430c1aSGuenter Roeck /*
13586430c1aSGuenter Roeck  * The complex conditional is necessary to avoid a cyclic dependency
13686430c1aSGuenter Roeck  * between hwmon and thermal_sys modules.
13786430c1aSGuenter Roeck  */
138f3735332SDaniel Lezcano #ifdef CONFIG_THERMAL_OF
139d560168bSGuenter Roeck static int hwmon_thermal_get_temp(void *data, int *temp)
140d560168bSGuenter Roeck {
141d560168bSGuenter Roeck 	struct hwmon_thermal_data *tdata = data;
1423bf8bdcfSGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
143d560168bSGuenter Roeck 	int ret;
144d560168bSGuenter Roeck 	long t;
145d560168bSGuenter Roeck 
1463bf8bdcfSGuenter Roeck 	ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
147d560168bSGuenter Roeck 				     tdata->index, &t);
148d560168bSGuenter Roeck 	if (ret < 0)
149d560168bSGuenter Roeck 		return ret;
150d560168bSGuenter Roeck 
151d560168bSGuenter Roeck 	*temp = t;
152d560168bSGuenter Roeck 
153d560168bSGuenter Roeck 	return 0;
154d560168bSGuenter Roeck }
155d560168bSGuenter Roeck 
156c9920650SJulia Lawall static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
157d560168bSGuenter Roeck 	.get_temp = hwmon_thermal_get_temp,
158d560168bSGuenter Roeck };
159d560168bSGuenter Roeck 
160*1597b374SGuenter Roeck static void hwmon_thermal_remove_sensor(void *data)
161*1597b374SGuenter Roeck {
162*1597b374SGuenter Roeck 	list_del(data);
163*1597b374SGuenter Roeck }
164*1597b374SGuenter Roeck 
1653bf8bdcfSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, int index)
166d560168bSGuenter Roeck {
167*1597b374SGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(dev);
168d560168bSGuenter Roeck 	struct hwmon_thermal_data *tdata;
16947c332deSLinus Walleij 	struct thermal_zone_device *tzd;
170*1597b374SGuenter Roeck 	int err;
171d560168bSGuenter Roeck 
172d560168bSGuenter Roeck 	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
173d560168bSGuenter Roeck 	if (!tdata)
174d560168bSGuenter Roeck 		return -ENOMEM;
175d560168bSGuenter Roeck 
1763bf8bdcfSGuenter Roeck 	tdata->dev = dev;
177d560168bSGuenter Roeck 	tdata->index = index;
178d560168bSGuenter Roeck 
1793bf8bdcfSGuenter Roeck 	tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata,
180d560168bSGuenter Roeck 						   &hwmon_thermal_ops);
18147c332deSLinus Walleij 	/*
18247c332deSLinus Walleij 	 * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV,
18347c332deSLinus Walleij 	 * so ignore that error but forward any other error.
18447c332deSLinus Walleij 	 */
18547c332deSLinus Walleij 	if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV))
18647c332deSLinus Walleij 		return PTR_ERR(tzd);
187d560168bSGuenter Roeck 
188*1597b374SGuenter Roeck 	err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node);
189*1597b374SGuenter Roeck 	if (err)
190*1597b374SGuenter Roeck 		return err;
191*1597b374SGuenter Roeck 
192*1597b374SGuenter Roeck 	tdata->tzd = tzd;
193*1597b374SGuenter Roeck 	list_add(&tdata->node, &hwdev->tzdata);
194*1597b374SGuenter Roeck 
195d560168bSGuenter Roeck 	return 0;
196d560168bSGuenter Roeck }
19744e3ad88SAkinobu Mita 
19844e3ad88SAkinobu Mita static int hwmon_thermal_register_sensors(struct device *dev)
19944e3ad88SAkinobu Mita {
20044e3ad88SAkinobu Mita 	struct hwmon_device *hwdev = to_hwmon_device(dev);
20144e3ad88SAkinobu Mita 	const struct hwmon_chip_info *chip = hwdev->chip;
20244e3ad88SAkinobu Mita 	const struct hwmon_channel_info **info = chip->info;
20344e3ad88SAkinobu Mita 	void *drvdata = dev_get_drvdata(dev);
20444e3ad88SAkinobu Mita 	int i;
20544e3ad88SAkinobu Mita 
20644e3ad88SAkinobu Mita 	for (i = 1; info[i]; i++) {
20744e3ad88SAkinobu Mita 		int j;
20844e3ad88SAkinobu Mita 
20944e3ad88SAkinobu Mita 		if (info[i]->type != hwmon_temp)
21044e3ad88SAkinobu Mita 			continue;
21144e3ad88SAkinobu Mita 
21244e3ad88SAkinobu Mita 		for (j = 0; info[i]->config[j]; j++) {
21344e3ad88SAkinobu Mita 			int err;
21444e3ad88SAkinobu Mita 
21544e3ad88SAkinobu Mita 			if (!(info[i]->config[j] & HWMON_T_INPUT) ||
21644e3ad88SAkinobu Mita 			    !chip->ops->is_visible(drvdata, hwmon_temp,
21744e3ad88SAkinobu Mita 						   hwmon_temp_input, j))
21844e3ad88SAkinobu Mita 				continue;
21944e3ad88SAkinobu Mita 
22044e3ad88SAkinobu Mita 			err = hwmon_thermal_add_sensor(dev, j);
22144e3ad88SAkinobu Mita 			if (err)
22244e3ad88SAkinobu Mita 				return err;
22344e3ad88SAkinobu Mita 		}
22444e3ad88SAkinobu Mita 	}
22544e3ad88SAkinobu Mita 
22644e3ad88SAkinobu Mita 	return 0;
22744e3ad88SAkinobu Mita }
22844e3ad88SAkinobu Mita 
229*1597b374SGuenter Roeck static void hwmon_thermal_notify(struct device *dev, int index)
230*1597b374SGuenter Roeck {
231*1597b374SGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(dev);
232*1597b374SGuenter Roeck 	struct hwmon_thermal_data *tzdata;
233*1597b374SGuenter Roeck 
234*1597b374SGuenter Roeck 	list_for_each_entry(tzdata, &hwdev->tzdata, node) {
235*1597b374SGuenter Roeck 		if (tzdata->index == index) {
236*1597b374SGuenter Roeck 			thermal_zone_device_update(tzdata->tzd,
237*1597b374SGuenter Roeck 						   THERMAL_EVENT_UNSPECIFIED);
238*1597b374SGuenter Roeck 		}
239*1597b374SGuenter Roeck 	}
240*1597b374SGuenter Roeck }
241*1597b374SGuenter Roeck 
242d560168bSGuenter Roeck #else
24344e3ad88SAkinobu Mita static int hwmon_thermal_register_sensors(struct device *dev)
244d560168bSGuenter Roeck {
245d560168bSGuenter Roeck 	return 0;
246d560168bSGuenter Roeck }
247*1597b374SGuenter Roeck 
248*1597b374SGuenter Roeck static void hwmon_thermal_notify(struct device *dev, int index) { }
249*1597b374SGuenter Roeck 
25086430c1aSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
251d560168bSGuenter Roeck 
25261b8ab2cSNicolin Chen static int hwmon_attr_base(enum hwmon_sensor_types type)
25361b8ab2cSNicolin Chen {
2544413405fSDr. David Alan Gilbert 	if (type == hwmon_in || type == hwmon_intrusion)
25561b8ab2cSNicolin Chen 		return 0;
25661b8ab2cSNicolin Chen 	return 1;
25761b8ab2cSNicolin Chen }
25861b8ab2cSNicolin Chen 
259d560168bSGuenter Roeck /* sysfs attribute management */
260d560168bSGuenter Roeck 
261d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev,
262d560168bSGuenter Roeck 			       struct device_attribute *devattr, char *buf)
263d560168bSGuenter Roeck {
264d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
265d560168bSGuenter Roeck 	long val;
266d560168bSGuenter Roeck 	int ret;
267d560168bSGuenter Roeck 
268d560168bSGuenter Roeck 	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
269d560168bSGuenter Roeck 			       &val);
270d560168bSGuenter Roeck 	if (ret < 0)
271d560168bSGuenter Roeck 		return ret;
272d560168bSGuenter Roeck 
27361b8ab2cSNicolin Chen 	trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
27461b8ab2cSNicolin Chen 			      hattr->name, val);
27561b8ab2cSNicolin Chen 
276d560168bSGuenter Roeck 	return sprintf(buf, "%ld\n", val);
277d560168bSGuenter Roeck }
278d560168bSGuenter Roeck 
279e159ab5cSGuenter Roeck static ssize_t hwmon_attr_show_string(struct device *dev,
280e159ab5cSGuenter Roeck 				      struct device_attribute *devattr,
281e159ab5cSGuenter Roeck 				      char *buf)
282e159ab5cSGuenter Roeck {
283e159ab5cSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
28461b8ab2cSNicolin Chen 	enum hwmon_sensor_types type = hattr->type;
2855ba6bcbcSJean Delvare 	const char *s;
286e159ab5cSGuenter Roeck 	int ret;
287e159ab5cSGuenter Roeck 
288e159ab5cSGuenter Roeck 	ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
289e159ab5cSGuenter Roeck 				      hattr->index, &s);
290e159ab5cSGuenter Roeck 	if (ret < 0)
291e159ab5cSGuenter Roeck 		return ret;
292e159ab5cSGuenter Roeck 
29361b8ab2cSNicolin Chen 	trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
29461b8ab2cSNicolin Chen 				     hattr->name, s);
29561b8ab2cSNicolin Chen 
296e159ab5cSGuenter Roeck 	return sprintf(buf, "%s\n", s);
297e159ab5cSGuenter Roeck }
298e159ab5cSGuenter Roeck 
299d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev,
300d560168bSGuenter Roeck 				struct device_attribute *devattr,
301d560168bSGuenter Roeck 				const char *buf, size_t count)
302d560168bSGuenter Roeck {
303d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
304d560168bSGuenter Roeck 	long val;
305d560168bSGuenter Roeck 	int ret;
306d560168bSGuenter Roeck 
307d560168bSGuenter Roeck 	ret = kstrtol(buf, 10, &val);
308d560168bSGuenter Roeck 	if (ret < 0)
309d560168bSGuenter Roeck 		return ret;
310d560168bSGuenter Roeck 
311d560168bSGuenter Roeck 	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
312d560168bSGuenter Roeck 				val);
313d560168bSGuenter Roeck 	if (ret < 0)
314d560168bSGuenter Roeck 		return ret;
315d560168bSGuenter Roeck 
31661b8ab2cSNicolin Chen 	trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
31761b8ab2cSNicolin Chen 			       hattr->name, val);
318d560168bSGuenter Roeck 
31961b8ab2cSNicolin Chen 	return count;
320d560168bSGuenter Roeck }
321d560168bSGuenter Roeck 
322e159ab5cSGuenter Roeck static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
323e159ab5cSGuenter Roeck {
324e159ab5cSGuenter Roeck 	return (type == hwmon_temp && attr == hwmon_temp_label) ||
325e159ab5cSGuenter Roeck 	       (type == hwmon_in && attr == hwmon_in_label) ||
326e159ab5cSGuenter Roeck 	       (type == hwmon_curr && attr == hwmon_curr_label) ||
327e159ab5cSGuenter Roeck 	       (type == hwmon_power && attr == hwmon_power_label) ||
328e159ab5cSGuenter Roeck 	       (type == hwmon_energy && attr == hwmon_energy_label) ||
329e159ab5cSGuenter Roeck 	       (type == hwmon_humidity && attr == hwmon_humidity_label) ||
330e159ab5cSGuenter Roeck 	       (type == hwmon_fan && attr == hwmon_fan_label);
331e159ab5cSGuenter Roeck }
332e159ab5cSGuenter Roeck 
3333bf8bdcfSGuenter Roeck static struct attribute *hwmon_genattr(const void *drvdata,
334d560168bSGuenter Roeck 				       enum hwmon_sensor_types type,
335d560168bSGuenter Roeck 				       u32 attr,
336d560168bSGuenter Roeck 				       int index,
337d560168bSGuenter Roeck 				       const char *template,
338d560168bSGuenter Roeck 				       const struct hwmon_ops *ops)
339d560168bSGuenter Roeck {
340d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr;
341d560168bSGuenter Roeck 	struct device_attribute *dattr;
342d560168bSGuenter Roeck 	struct attribute *a;
343d560168bSGuenter Roeck 	umode_t mode;
3443b443defSRasmus Villemoes 	const char *name;
345e159ab5cSGuenter Roeck 	bool is_string = is_string_attr(type, attr);
346d560168bSGuenter Roeck 
347d560168bSGuenter Roeck 	/* The attribute is invisible if there is no template string */
348d560168bSGuenter Roeck 	if (!template)
349d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
350d560168bSGuenter Roeck 
351d560168bSGuenter Roeck 	mode = ops->is_visible(drvdata, type, attr, index);
352d560168bSGuenter Roeck 	if (!mode)
353d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
354d560168bSGuenter Roeck 
3550d87116fSGuenter Roeck 	if ((mode & 0444) && ((is_string && !ops->read_string) ||
356e159ab5cSGuenter Roeck 				 (!is_string && !ops->read)))
357d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
3580d87116fSGuenter Roeck 	if ((mode & 0222) && !ops->write)
359d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
360d560168bSGuenter Roeck 
3613bf8bdcfSGuenter Roeck 	hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
362d560168bSGuenter Roeck 	if (!hattr)
363d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
364d560168bSGuenter Roeck 
3653a412d5eSGuenter Roeck 	if (type == hwmon_chip) {
3663b443defSRasmus Villemoes 		name = template;
3673a412d5eSGuenter Roeck 	} else {
3683a412d5eSGuenter Roeck 		scnprintf(hattr->name, sizeof(hattr->name), template,
3693a412d5eSGuenter Roeck 			  index + hwmon_attr_base(type));
3703a412d5eSGuenter Roeck 		name = hattr->name;
3713a412d5eSGuenter Roeck 	}
3723a412d5eSGuenter Roeck 
373d560168bSGuenter Roeck 	hattr->type = type;
374d560168bSGuenter Roeck 	hattr->attr = attr;
375d560168bSGuenter Roeck 	hattr->index = index;
376d560168bSGuenter Roeck 	hattr->ops = ops;
377d560168bSGuenter Roeck 
378d560168bSGuenter Roeck 	dattr = &hattr->dev_attr;
379e159ab5cSGuenter Roeck 	dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
380d560168bSGuenter Roeck 	dattr->store = hwmon_attr_store;
381d560168bSGuenter Roeck 
382d560168bSGuenter Roeck 	a = &dattr->attr;
383d560168bSGuenter Roeck 	sysfs_attr_init(a);
384d560168bSGuenter Roeck 	a->name = name;
385d560168bSGuenter Roeck 	a->mode = mode;
386d560168bSGuenter Roeck 
387d560168bSGuenter Roeck 	return a;
388d560168bSGuenter Roeck }
389d560168bSGuenter Roeck 
390f4d325d5SGuenter Roeck /*
391f4d325d5SGuenter Roeck  * Chip attributes are not attribute templates but actual sysfs attributes.
392f4d325d5SGuenter Roeck  * See hwmon_genattr() for special handling.
393f4d325d5SGuenter Roeck  */
394f4d325d5SGuenter Roeck static const char * const hwmon_chip_attrs[] = {
395d560168bSGuenter Roeck 	[hwmon_chip_temp_reset_history] = "temp_reset_history",
39600d616cfSGuenter Roeck 	[hwmon_chip_in_reset_history] = "in_reset_history",
3979b26947cSGuenter Roeck 	[hwmon_chip_curr_reset_history] = "curr_reset_history",
398b308f5c7SGuenter Roeck 	[hwmon_chip_power_reset_history] = "power_reset_history",
399d560168bSGuenter Roeck 	[hwmon_chip_update_interval] = "update_interval",
400d560168bSGuenter Roeck 	[hwmon_chip_alarms] = "alarms",
4019f00995eSGuenter Roeck 	[hwmon_chip_samples] = "samples",
4029f00995eSGuenter Roeck 	[hwmon_chip_curr_samples] = "curr_samples",
4039f00995eSGuenter Roeck 	[hwmon_chip_in_samples] = "in_samples",
4049f00995eSGuenter Roeck 	[hwmon_chip_power_samples] = "power_samples",
4059f00995eSGuenter Roeck 	[hwmon_chip_temp_samples] = "temp_samples",
406d560168bSGuenter Roeck };
407d560168bSGuenter Roeck 
408d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = {
409002c6b54SGuenter Roeck 	[hwmon_temp_enable] = "temp%d_enable",
410d560168bSGuenter Roeck 	[hwmon_temp_input] = "temp%d_input",
411d560168bSGuenter Roeck 	[hwmon_temp_type] = "temp%d_type",
412d560168bSGuenter Roeck 	[hwmon_temp_lcrit] = "temp%d_lcrit",
413d560168bSGuenter Roeck 	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
414d560168bSGuenter Roeck 	[hwmon_temp_min] = "temp%d_min",
415d560168bSGuenter Roeck 	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
416d560168bSGuenter Roeck 	[hwmon_temp_max] = "temp%d_max",
417d560168bSGuenter Roeck 	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
418d560168bSGuenter Roeck 	[hwmon_temp_crit] = "temp%d_crit",
419d560168bSGuenter Roeck 	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
420d560168bSGuenter Roeck 	[hwmon_temp_emergency] = "temp%d_emergency",
421d560168bSGuenter Roeck 	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
422d560168bSGuenter Roeck 	[hwmon_temp_alarm] = "temp%d_alarm",
423d560168bSGuenter Roeck 	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
424d560168bSGuenter Roeck 	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
425d560168bSGuenter Roeck 	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
426d560168bSGuenter Roeck 	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
427d560168bSGuenter Roeck 	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
428d560168bSGuenter Roeck 	[hwmon_temp_fault] = "temp%d_fault",
429d560168bSGuenter Roeck 	[hwmon_temp_offset] = "temp%d_offset",
430d560168bSGuenter Roeck 	[hwmon_temp_label] = "temp%d_label",
431d560168bSGuenter Roeck 	[hwmon_temp_lowest] = "temp%d_lowest",
432d560168bSGuenter Roeck 	[hwmon_temp_highest] = "temp%d_highest",
433d560168bSGuenter Roeck 	[hwmon_temp_reset_history] = "temp%d_reset_history",
434d560168bSGuenter Roeck };
435d560168bSGuenter Roeck 
43600d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = {
437002c6b54SGuenter Roeck 	[hwmon_in_enable] = "in%d_enable",
43800d616cfSGuenter Roeck 	[hwmon_in_input] = "in%d_input",
43900d616cfSGuenter Roeck 	[hwmon_in_min] = "in%d_min",
44000d616cfSGuenter Roeck 	[hwmon_in_max] = "in%d_max",
44100d616cfSGuenter Roeck 	[hwmon_in_lcrit] = "in%d_lcrit",
44200d616cfSGuenter Roeck 	[hwmon_in_crit] = "in%d_crit",
44300d616cfSGuenter Roeck 	[hwmon_in_average] = "in%d_average",
44400d616cfSGuenter Roeck 	[hwmon_in_lowest] = "in%d_lowest",
44500d616cfSGuenter Roeck 	[hwmon_in_highest] = "in%d_highest",
44600d616cfSGuenter Roeck 	[hwmon_in_reset_history] = "in%d_reset_history",
44700d616cfSGuenter Roeck 	[hwmon_in_label] = "in%d_label",
44800d616cfSGuenter Roeck 	[hwmon_in_alarm] = "in%d_alarm",
44900d616cfSGuenter Roeck 	[hwmon_in_min_alarm] = "in%d_min_alarm",
45000d616cfSGuenter Roeck 	[hwmon_in_max_alarm] = "in%d_max_alarm",
45100d616cfSGuenter Roeck 	[hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
45200d616cfSGuenter Roeck 	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
45300d616cfSGuenter Roeck };
45400d616cfSGuenter Roeck 
4559b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = {
456002c6b54SGuenter Roeck 	[hwmon_curr_enable] = "curr%d_enable",
4579b26947cSGuenter Roeck 	[hwmon_curr_input] = "curr%d_input",
4589b26947cSGuenter Roeck 	[hwmon_curr_min] = "curr%d_min",
4599b26947cSGuenter Roeck 	[hwmon_curr_max] = "curr%d_max",
4609b26947cSGuenter Roeck 	[hwmon_curr_lcrit] = "curr%d_lcrit",
4619b26947cSGuenter Roeck 	[hwmon_curr_crit] = "curr%d_crit",
4629b26947cSGuenter Roeck 	[hwmon_curr_average] = "curr%d_average",
4639b26947cSGuenter Roeck 	[hwmon_curr_lowest] = "curr%d_lowest",
4649b26947cSGuenter Roeck 	[hwmon_curr_highest] = "curr%d_highest",
4659b26947cSGuenter Roeck 	[hwmon_curr_reset_history] = "curr%d_reset_history",
4669b26947cSGuenter Roeck 	[hwmon_curr_label] = "curr%d_label",
4679b26947cSGuenter Roeck 	[hwmon_curr_alarm] = "curr%d_alarm",
4689b26947cSGuenter Roeck 	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
4699b26947cSGuenter Roeck 	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
4709b26947cSGuenter Roeck 	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
4719b26947cSGuenter Roeck 	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
4729b26947cSGuenter Roeck };
4739b26947cSGuenter Roeck 
474b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = {
475002c6b54SGuenter Roeck 	[hwmon_power_enable] = "power%d_enable",
476b308f5c7SGuenter Roeck 	[hwmon_power_average] = "power%d_average",
477b308f5c7SGuenter Roeck 	[hwmon_power_average_interval] = "power%d_average_interval",
478b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_max] = "power%d_interval_max",
479b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_min] = "power%d_interval_min",
480b308f5c7SGuenter Roeck 	[hwmon_power_average_highest] = "power%d_average_highest",
481b308f5c7SGuenter Roeck 	[hwmon_power_average_lowest] = "power%d_average_lowest",
482b308f5c7SGuenter Roeck 	[hwmon_power_average_max] = "power%d_average_max",
483b308f5c7SGuenter Roeck 	[hwmon_power_average_min] = "power%d_average_min",
484b308f5c7SGuenter Roeck 	[hwmon_power_input] = "power%d_input",
485b308f5c7SGuenter Roeck 	[hwmon_power_input_highest] = "power%d_input_highest",
486b308f5c7SGuenter Roeck 	[hwmon_power_input_lowest] = "power%d_input_lowest",
487b308f5c7SGuenter Roeck 	[hwmon_power_reset_history] = "power%d_reset_history",
488b308f5c7SGuenter Roeck 	[hwmon_power_accuracy] = "power%d_accuracy",
489b308f5c7SGuenter Roeck 	[hwmon_power_cap] = "power%d_cap",
490b308f5c7SGuenter Roeck 	[hwmon_power_cap_hyst] = "power%d_cap_hyst",
491b308f5c7SGuenter Roeck 	[hwmon_power_cap_max] = "power%d_cap_max",
492b308f5c7SGuenter Roeck 	[hwmon_power_cap_min] = "power%d_cap_min",
493aa7f29b0SAndrew Lunn 	[hwmon_power_min] = "power%d_min",
494b308f5c7SGuenter Roeck 	[hwmon_power_max] = "power%d_max",
495aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit] = "power%d_lcrit",
496b308f5c7SGuenter Roeck 	[hwmon_power_crit] = "power%d_crit",
497b308f5c7SGuenter Roeck 	[hwmon_power_label] = "power%d_label",
498b308f5c7SGuenter Roeck 	[hwmon_power_alarm] = "power%d_alarm",
499b308f5c7SGuenter Roeck 	[hwmon_power_cap_alarm] = "power%d_cap_alarm",
500aa7f29b0SAndrew Lunn 	[hwmon_power_min_alarm] = "power%d_min_alarm",
501b308f5c7SGuenter Roeck 	[hwmon_power_max_alarm] = "power%d_max_alarm",
502aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
503b308f5c7SGuenter Roeck 	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
504b308f5c7SGuenter Roeck };
505b308f5c7SGuenter Roeck 
5066bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = {
507002c6b54SGuenter Roeck 	[hwmon_energy_enable] = "energy%d_enable",
5086bfcca44SGuenter Roeck 	[hwmon_energy_input] = "energy%d_input",
5096bfcca44SGuenter Roeck 	[hwmon_energy_label] = "energy%d_label",
5106bfcca44SGuenter Roeck };
5116bfcca44SGuenter Roeck 
5126bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = {
513002c6b54SGuenter Roeck 	[hwmon_humidity_enable] = "humidity%d_enable",
5146bfcca44SGuenter Roeck 	[hwmon_humidity_input] = "humidity%d_input",
5156bfcca44SGuenter Roeck 	[hwmon_humidity_label] = "humidity%d_label",
5166bfcca44SGuenter Roeck 	[hwmon_humidity_min] = "humidity%d_min",
5176bfcca44SGuenter Roeck 	[hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
5186bfcca44SGuenter Roeck 	[hwmon_humidity_max] = "humidity%d_max",
5196bfcca44SGuenter Roeck 	[hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
5206bfcca44SGuenter Roeck 	[hwmon_humidity_alarm] = "humidity%d_alarm",
5216bfcca44SGuenter Roeck 	[hwmon_humidity_fault] = "humidity%d_fault",
5226bfcca44SGuenter Roeck };
5236bfcca44SGuenter Roeck 
5248faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = {
525002c6b54SGuenter Roeck 	[hwmon_fan_enable] = "fan%d_enable",
5268faee73fSGuenter Roeck 	[hwmon_fan_input] = "fan%d_input",
5278faee73fSGuenter Roeck 	[hwmon_fan_label] = "fan%d_label",
5288faee73fSGuenter Roeck 	[hwmon_fan_min] = "fan%d_min",
5298faee73fSGuenter Roeck 	[hwmon_fan_max] = "fan%d_max",
5308faee73fSGuenter Roeck 	[hwmon_fan_div] = "fan%d_div",
5318faee73fSGuenter Roeck 	[hwmon_fan_pulses] = "fan%d_pulses",
5328faee73fSGuenter Roeck 	[hwmon_fan_target] = "fan%d_target",
5338faee73fSGuenter Roeck 	[hwmon_fan_alarm] = "fan%d_alarm",
5348faee73fSGuenter Roeck 	[hwmon_fan_min_alarm] = "fan%d_min_alarm",
5358faee73fSGuenter Roeck 	[hwmon_fan_max_alarm] = "fan%d_max_alarm",
5368faee73fSGuenter Roeck 	[hwmon_fan_fault] = "fan%d_fault",
5378faee73fSGuenter Roeck };
5388faee73fSGuenter Roeck 
539f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = {
540f9f7bb3aSGuenter Roeck 	[hwmon_pwm_input] = "pwm%d",
541f9f7bb3aSGuenter Roeck 	[hwmon_pwm_enable] = "pwm%d_enable",
542f9f7bb3aSGuenter Roeck 	[hwmon_pwm_mode] = "pwm%d_mode",
543f9f7bb3aSGuenter Roeck 	[hwmon_pwm_freq] = "pwm%d_freq",
544f9f7bb3aSGuenter Roeck };
545f9f7bb3aSGuenter Roeck 
5464413405fSDr. David Alan Gilbert static const char * const hwmon_intrusion_attr_templates[] = {
5474413405fSDr. David Alan Gilbert 	[hwmon_intrusion_alarm] = "intrusion%d_alarm",
5484413405fSDr. David Alan Gilbert 	[hwmon_intrusion_beep]  = "intrusion%d_beep",
5494413405fSDr. David Alan Gilbert };
5504413405fSDr. David Alan Gilbert 
551d560168bSGuenter Roeck static const char * const *__templates[] = {
552f4d325d5SGuenter Roeck 	[hwmon_chip] = hwmon_chip_attrs,
553d560168bSGuenter Roeck 	[hwmon_temp] = hwmon_temp_attr_templates,
55400d616cfSGuenter Roeck 	[hwmon_in] = hwmon_in_attr_templates,
5559b26947cSGuenter Roeck 	[hwmon_curr] = hwmon_curr_attr_templates,
556b308f5c7SGuenter Roeck 	[hwmon_power] = hwmon_power_attr_templates,
5576bfcca44SGuenter Roeck 	[hwmon_energy] = hwmon_energy_attr_templates,
5586bfcca44SGuenter Roeck 	[hwmon_humidity] = hwmon_humidity_attr_templates,
5598faee73fSGuenter Roeck 	[hwmon_fan] = hwmon_fan_attr_templates,
560f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = hwmon_pwm_attr_templates,
5614413405fSDr. David Alan Gilbert 	[hwmon_intrusion] = hwmon_intrusion_attr_templates,
562d560168bSGuenter Roeck };
563d560168bSGuenter Roeck 
564d560168bSGuenter Roeck static const int __templates_size[] = {
565f4d325d5SGuenter Roeck 	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
566d560168bSGuenter Roeck 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
56700d616cfSGuenter Roeck 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
5689b26947cSGuenter Roeck 	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
569b308f5c7SGuenter Roeck 	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
5706bfcca44SGuenter Roeck 	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
5716bfcca44SGuenter Roeck 	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
5728faee73fSGuenter Roeck 	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
573f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
5744413405fSDr. David Alan Gilbert 	[hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
575d560168bSGuenter Roeck };
576d560168bSGuenter Roeck 
577*1597b374SGuenter Roeck int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
578*1597b374SGuenter Roeck 		       u32 attr, int channel)
579*1597b374SGuenter Roeck {
580*1597b374SGuenter Roeck 	char sattr[MAX_SYSFS_ATTR_NAME_LENGTH];
581*1597b374SGuenter Roeck 	const char * const *templates;
582*1597b374SGuenter Roeck 	const char *template;
583*1597b374SGuenter Roeck 	int base;
584*1597b374SGuenter Roeck 
585*1597b374SGuenter Roeck 	if (type >= ARRAY_SIZE(__templates))
586*1597b374SGuenter Roeck 		return -EINVAL;
587*1597b374SGuenter Roeck 	if (attr >= __templates_size[type])
588*1597b374SGuenter Roeck 		return -EINVAL;
589*1597b374SGuenter Roeck 
590*1597b374SGuenter Roeck 	templates = __templates[type];
591*1597b374SGuenter Roeck 	template = templates[attr];
592*1597b374SGuenter Roeck 
593*1597b374SGuenter Roeck 	base = hwmon_attr_base(type);
594*1597b374SGuenter Roeck 
595*1597b374SGuenter Roeck 	scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel);
596*1597b374SGuenter Roeck 	sysfs_notify(&dev->kobj, NULL, sattr);
597*1597b374SGuenter Roeck 	kobject_uevent(&dev->kobj, KOBJ_CHANGE);
598*1597b374SGuenter Roeck 
599*1597b374SGuenter Roeck 	if (type == hwmon_temp)
600*1597b374SGuenter Roeck 		hwmon_thermal_notify(dev, channel);
601*1597b374SGuenter Roeck 
602*1597b374SGuenter Roeck 	return 0;
603*1597b374SGuenter Roeck }
604*1597b374SGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_notify_event);
605*1597b374SGuenter Roeck 
606d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
607d560168bSGuenter Roeck {
608d560168bSGuenter Roeck 	int i, n;
609d560168bSGuenter Roeck 
610d560168bSGuenter Roeck 	for (i = n = 0; info->config[i]; i++)
611d560168bSGuenter Roeck 		n += hweight32(info->config[i]);
612d560168bSGuenter Roeck 
613d560168bSGuenter Roeck 	return n;
614d560168bSGuenter Roeck }
615d560168bSGuenter Roeck 
6163bf8bdcfSGuenter Roeck static int hwmon_genattrs(const void *drvdata,
617d560168bSGuenter Roeck 			  struct attribute **attrs,
618d560168bSGuenter Roeck 			  const struct hwmon_ops *ops,
619d560168bSGuenter Roeck 			  const struct hwmon_channel_info *info)
620d560168bSGuenter Roeck {
621d560168bSGuenter Roeck 	const char * const *templates;
622d560168bSGuenter Roeck 	int template_size;
623d560168bSGuenter Roeck 	int i, aindex = 0;
624d560168bSGuenter Roeck 
625d560168bSGuenter Roeck 	if (info->type >= ARRAY_SIZE(__templates))
626d560168bSGuenter Roeck 		return -EINVAL;
627d560168bSGuenter Roeck 
628d560168bSGuenter Roeck 	templates = __templates[info->type];
629d560168bSGuenter Roeck 	template_size = __templates_size[info->type];
630d560168bSGuenter Roeck 
631d560168bSGuenter Roeck 	for (i = 0; info->config[i]; i++) {
632d560168bSGuenter Roeck 		u32 attr_mask = info->config[i];
633d560168bSGuenter Roeck 		u32 attr;
634d560168bSGuenter Roeck 
635d560168bSGuenter Roeck 		while (attr_mask) {
636d560168bSGuenter Roeck 			struct attribute *a;
637d560168bSGuenter Roeck 
638d560168bSGuenter Roeck 			attr = __ffs(attr_mask);
639d560168bSGuenter Roeck 			attr_mask &= ~BIT(attr);
640d560168bSGuenter Roeck 			if (attr >= template_size)
641d560168bSGuenter Roeck 				return -EINVAL;
6423bf8bdcfSGuenter Roeck 			a = hwmon_genattr(drvdata, info->type, attr, i,
643d560168bSGuenter Roeck 					  templates[attr], ops);
644d560168bSGuenter Roeck 			if (IS_ERR(a)) {
645d560168bSGuenter Roeck 				if (PTR_ERR(a) != -ENOENT)
646d560168bSGuenter Roeck 					return PTR_ERR(a);
647d560168bSGuenter Roeck 				continue;
648d560168bSGuenter Roeck 			}
649d560168bSGuenter Roeck 			attrs[aindex++] = a;
650d560168bSGuenter Roeck 		}
651d560168bSGuenter Roeck 	}
652d560168bSGuenter Roeck 	return aindex;
653d560168bSGuenter Roeck }
654d560168bSGuenter Roeck 
655d560168bSGuenter Roeck static struct attribute **
6563bf8bdcfSGuenter Roeck __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
657d560168bSGuenter Roeck {
658d560168bSGuenter Roeck 	int ret, i, aindex = 0, nattrs = 0;
659d560168bSGuenter Roeck 	struct attribute **attrs;
660d560168bSGuenter Roeck 
661d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++)
662d560168bSGuenter Roeck 		nattrs += hwmon_num_channel_attrs(chip->info[i]);
663d560168bSGuenter Roeck 
664d560168bSGuenter Roeck 	if (nattrs == 0)
665d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
666d560168bSGuenter Roeck 
6673bf8bdcfSGuenter Roeck 	attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
668d560168bSGuenter Roeck 	if (!attrs)
669d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
670d560168bSGuenter Roeck 
671d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++) {
6723bf8bdcfSGuenter Roeck 		ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
673d560168bSGuenter Roeck 				     chip->info[i]);
6743bf8bdcfSGuenter Roeck 		if (ret < 0) {
6753bf8bdcfSGuenter Roeck 			hwmon_free_attrs(attrs);
676d560168bSGuenter Roeck 			return ERR_PTR(ret);
6773bf8bdcfSGuenter Roeck 		}
678d560168bSGuenter Roeck 		aindex += ret;
679d560168bSGuenter Roeck 	}
680d560168bSGuenter Roeck 
681d560168bSGuenter Roeck 	return attrs;
682d560168bSGuenter Roeck }
683d560168bSGuenter Roeck 
684d560168bSGuenter Roeck static struct device *
685d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
686d560168bSGuenter Roeck 			const struct hwmon_chip_info *chip,
687d560168bSGuenter Roeck 			const struct attribute_group **groups)
688d560168bSGuenter Roeck {
689d560168bSGuenter Roeck 	struct hwmon_device *hwdev;
690d560168bSGuenter Roeck 	struct device *hdev;
69144e3ad88SAkinobu Mita 	int i, err, id;
692d560168bSGuenter Roeck 
69374d3b641SGuenter Roeck 	/* Complain about invalid characters in hwmon name attribute */
694d560168bSGuenter Roeck 	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
69574d3b641SGuenter Roeck 		dev_warn(dev,
69674d3b641SGuenter Roeck 			 "hwmon: '%s' is not a valid name attribute, please fix\n",
69774d3b641SGuenter Roeck 			 name);
698d560168bSGuenter Roeck 
699d560168bSGuenter Roeck 	id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
700d560168bSGuenter Roeck 	if (id < 0)
701d560168bSGuenter Roeck 		return ERR_PTR(id);
702d560168bSGuenter Roeck 
703d560168bSGuenter Roeck 	hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
704d560168bSGuenter Roeck 	if (hwdev == NULL) {
705d560168bSGuenter Roeck 		err = -ENOMEM;
706d560168bSGuenter Roeck 		goto ida_remove;
707d560168bSGuenter Roeck 	}
708d560168bSGuenter Roeck 
709d560168bSGuenter Roeck 	hdev = &hwdev->dev;
710d560168bSGuenter Roeck 
711239552f4SGuenter Roeck 	if (chip) {
712d560168bSGuenter Roeck 		struct attribute **attrs;
713b2a4cc3aSGuenter Roeck 		int ngroups = 2; /* terminating NULL plus &hwdev->groups */
714d560168bSGuenter Roeck 
715d560168bSGuenter Roeck 		if (groups)
716d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
717d560168bSGuenter Roeck 				ngroups++;
718d560168bSGuenter Roeck 
7193bf8bdcfSGuenter Roeck 		hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
72038d8ed65SColin Ian King 		if (!hwdev->groups) {
72138d8ed65SColin Ian King 			err = -ENOMEM;
72238d8ed65SColin Ian King 			goto free_hwmon;
72338d8ed65SColin Ian King 		}
724d560168bSGuenter Roeck 
7253bf8bdcfSGuenter Roeck 		attrs = __hwmon_create_attrs(drvdata, chip);
726d560168bSGuenter Roeck 		if (IS_ERR(attrs)) {
727d560168bSGuenter Roeck 			err = PTR_ERR(attrs);
728d560168bSGuenter Roeck 			goto free_hwmon;
729d560168bSGuenter Roeck 		}
730d560168bSGuenter Roeck 
731d560168bSGuenter Roeck 		hwdev->group.attrs = attrs;
732d560168bSGuenter Roeck 		ngroups = 0;
733d560168bSGuenter Roeck 		hwdev->groups[ngroups++] = &hwdev->group;
734d560168bSGuenter Roeck 
735d560168bSGuenter Roeck 		if (groups) {
736d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
737d560168bSGuenter Roeck 				hwdev->groups[ngroups++] = groups[i];
738d560168bSGuenter Roeck 		}
739d560168bSGuenter Roeck 
740d560168bSGuenter Roeck 		hdev->groups = hwdev->groups;
741d560168bSGuenter Roeck 	} else {
742d560168bSGuenter Roeck 		hdev->groups = groups;
743d560168bSGuenter Roeck 	}
744d560168bSGuenter Roeck 
745d560168bSGuenter Roeck 	hwdev->name = name;
746d560168bSGuenter Roeck 	hdev->class = &hwmon_class;
747d560168bSGuenter Roeck 	hdev->parent = dev;
748d560168bSGuenter Roeck 	hdev->of_node = dev ? dev->of_node : NULL;
749d560168bSGuenter Roeck 	hwdev->chip = chip;
750d560168bSGuenter Roeck 	dev_set_drvdata(hdev, drvdata);
751d560168bSGuenter Roeck 	dev_set_name(hdev, HWMON_ID_FORMAT, id);
752d560168bSGuenter Roeck 	err = device_register(hdev);
753d560168bSGuenter Roeck 	if (err)
754d560168bSGuenter Roeck 		goto free_hwmon;
755d560168bSGuenter Roeck 
756*1597b374SGuenter Roeck 	INIT_LIST_HEAD(&hwdev->tzdata);
757*1597b374SGuenter Roeck 
758c41dd48eSEduardo Valentin 	if (dev && dev->of_node && chip && chip->ops->read &&
759d560168bSGuenter Roeck 	    chip->info[0]->type == hwmon_chip &&
760d560168bSGuenter Roeck 	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
76144e3ad88SAkinobu Mita 		err = hwmon_thermal_register_sensors(hdev);
76274e35127SDmitry Osipenko 		if (err) {
76374e35127SDmitry Osipenko 			device_unregister(hdev);
764792eac18SGuenter Roeck 			/*
76544e3ad88SAkinobu Mita 			 * Don't worry about hwdev; hwmon_dev_release(), called
76644e3ad88SAkinobu Mita 			 * from device_unregister(), will free it.
767792eac18SGuenter Roeck 			 */
76874e35127SDmitry Osipenko 			goto ida_remove;
76974e35127SDmitry Osipenko 		}
77047c332deSLinus Walleij 	}
771d560168bSGuenter Roeck 
772d560168bSGuenter Roeck 	return hdev;
773d560168bSGuenter Roeck 
774d560168bSGuenter Roeck free_hwmon:
7753bf8bdcfSGuenter Roeck 	hwmon_dev_release(hdev);
776d560168bSGuenter Roeck ida_remove:
777d560168bSGuenter Roeck 	ida_simple_remove(&hwmon_ida, id);
778d560168bSGuenter Roeck 	return ERR_PTR(err);
779d560168bSGuenter Roeck }
780d560168bSGuenter Roeck 
7811236441fSMark M. Hoffman /**
782bab2243cSGuenter Roeck  * hwmon_device_register_with_groups - register w/ hwmon
783bab2243cSGuenter Roeck  * @dev: the parent device
784bab2243cSGuenter Roeck  * @name: hwmon name attribute
785bab2243cSGuenter Roeck  * @drvdata: driver data to attach to created device
786bab2243cSGuenter Roeck  * @groups: List of attribute groups to create
787bab2243cSGuenter Roeck  *
788bab2243cSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
789bab2243cSGuenter Roeck  * longer needed.
790bab2243cSGuenter Roeck  *
791bab2243cSGuenter Roeck  * Returns the pointer to the new device.
792bab2243cSGuenter Roeck  */
793bab2243cSGuenter Roeck struct device *
794bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name,
795bab2243cSGuenter Roeck 				  void *drvdata,
796bab2243cSGuenter Roeck 				  const struct attribute_group **groups)
797bab2243cSGuenter Roeck {
7988353863aSGuenter Roeck 	if (!name)
7998353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
8008353863aSGuenter Roeck 
801d560168bSGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
802bab2243cSGuenter Roeck }
803bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
804bab2243cSGuenter Roeck 
805bab2243cSGuenter Roeck /**
806d560168bSGuenter Roeck  * 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
811848ba0a2SGuenter Roeck  * @extra_groups: pointer to list of additional non-standard attribute groups
812d560168bSGuenter Roeck  *
813d560168bSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
814d560168bSGuenter Roeck  * longer needed.
815d560168bSGuenter Roeck  *
816d560168bSGuenter Roeck  * Returns the pointer to the new device.
817d560168bSGuenter Roeck  */
818d560168bSGuenter Roeck struct device *
819d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name,
820d560168bSGuenter Roeck 				void *drvdata,
821d560168bSGuenter Roeck 				const struct hwmon_chip_info *chip,
822848ba0a2SGuenter Roeck 				const struct attribute_group **extra_groups)
823d560168bSGuenter Roeck {
8248353863aSGuenter Roeck 	if (!name)
8258353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
8268353863aSGuenter Roeck 
827239552f4SGuenter Roeck 	if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
828d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
829d560168bSGuenter Roeck 
83059df4f4eSLucas Magasweran 	if (chip && !dev)
83159df4f4eSLucas Magasweran 		return ERR_PTR(-EINVAL);
83259df4f4eSLucas Magasweran 
833848ba0a2SGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
834d560168bSGuenter Roeck }
835d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
836d560168bSGuenter Roeck 
837d560168bSGuenter Roeck /**
8381beeffe4STony Jones  * hwmon_device_register - register w/ hwmon
8391236441fSMark M. Hoffman  * @dev: the device to register
8401236441fSMark M. Hoffman  *
8411beeffe4STony Jones  * hwmon_device_unregister() must be called when the device is no
8421236441fSMark M. Hoffman  * longer needed.
8431236441fSMark M. Hoffman  *
8441beeffe4STony Jones  * Returns the pointer to the new device.
8451236441fSMark M. Hoffman  */
8461beeffe4STony Jones struct device *hwmon_device_register(struct device *dev)
8471236441fSMark M. Hoffman {
848af1bd36cSGuenter Roeck 	dev_warn(dev,
849af1bd36cSGuenter Roeck 		 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
850af1bd36cSGuenter Roeck 
8518353863aSGuenter Roeck 	return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
8521236441fSMark M. Hoffman }
853839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register);
8541236441fSMark M. Hoffman 
8551236441fSMark M. Hoffman /**
8561236441fSMark M. Hoffman  * hwmon_device_unregister - removes the previously registered class device
8571236441fSMark M. Hoffman  *
8581beeffe4STony Jones  * @dev: the class device to destroy
8591236441fSMark M. Hoffman  */
8601beeffe4STony Jones void hwmon_device_unregister(struct device *dev)
8611236441fSMark M. Hoffman {
8621236441fSMark M. Hoffman 	int id;
8631236441fSMark M. Hoffman 
864739cf3a2SKay Sievers 	if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
8651beeffe4STony Jones 		device_unregister(dev);
8664ca5f468SJonathan Cameron 		ida_simple_remove(&hwmon_ida, id);
8671236441fSMark M. Hoffman 	} else
8681beeffe4STony Jones 		dev_dbg(dev->parent,
8691236441fSMark M. Hoffman 			"hwmon_device_unregister() failed: bad class ID!\n");
8701236441fSMark M. Hoffman }
871839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister);
8721236441fSMark M. Hoffman 
87374188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res)
87474188cbaSGuenter Roeck {
87574188cbaSGuenter Roeck 	struct device *hwdev = *(struct device **)res;
87674188cbaSGuenter Roeck 
87774188cbaSGuenter Roeck 	hwmon_device_unregister(hwdev);
87874188cbaSGuenter Roeck }
87974188cbaSGuenter Roeck 
88074188cbaSGuenter Roeck /**
88174188cbaSGuenter Roeck  * devm_hwmon_device_register_with_groups - register w/ hwmon
88274188cbaSGuenter Roeck  * @dev: the parent device
88374188cbaSGuenter Roeck  * @name: hwmon name attribute
88474188cbaSGuenter Roeck  * @drvdata: driver data to attach to created device
88574188cbaSGuenter Roeck  * @groups: List of attribute groups to create
88674188cbaSGuenter Roeck  *
88774188cbaSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
88874188cbaSGuenter Roeck  * unregistered with the parent device.
88974188cbaSGuenter Roeck  */
89074188cbaSGuenter Roeck struct device *
89174188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
89274188cbaSGuenter Roeck 				       void *drvdata,
89374188cbaSGuenter Roeck 				       const struct attribute_group **groups)
89474188cbaSGuenter Roeck {
89574188cbaSGuenter Roeck 	struct device **ptr, *hwdev;
89674188cbaSGuenter Roeck 
89774188cbaSGuenter Roeck 	if (!dev)
89874188cbaSGuenter Roeck 		return ERR_PTR(-EINVAL);
89974188cbaSGuenter Roeck 
90074188cbaSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
90174188cbaSGuenter Roeck 	if (!ptr)
90274188cbaSGuenter Roeck 		return ERR_PTR(-ENOMEM);
90374188cbaSGuenter Roeck 
90474188cbaSGuenter Roeck 	hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
90574188cbaSGuenter Roeck 	if (IS_ERR(hwdev))
90674188cbaSGuenter Roeck 		goto error;
90774188cbaSGuenter Roeck 
90874188cbaSGuenter Roeck 	*ptr = hwdev;
90974188cbaSGuenter Roeck 	devres_add(dev, ptr);
91074188cbaSGuenter Roeck 	return hwdev;
91174188cbaSGuenter Roeck 
91274188cbaSGuenter Roeck error:
91374188cbaSGuenter Roeck 	devres_free(ptr);
91474188cbaSGuenter Roeck 	return hwdev;
91574188cbaSGuenter Roeck }
91674188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
91774188cbaSGuenter Roeck 
918d560168bSGuenter Roeck /**
919d560168bSGuenter Roeck  * devm_hwmon_device_register_with_info - register w/ hwmon
920d560168bSGuenter Roeck  * @dev:	the parent device
921d560168bSGuenter Roeck  * @name:	hwmon name attribute
922d560168bSGuenter Roeck  * @drvdata:	driver data to attach to created device
9233870945aSGuenter Roeck  * @chip:	pointer to hwmon chip information
9243870945aSGuenter Roeck  * @groups:	pointer to list of driver specific attribute groups
925d560168bSGuenter Roeck  *
926d560168bSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
927d560168bSGuenter Roeck  * unregistered with the parent device.
928d560168bSGuenter Roeck  */
929d560168bSGuenter Roeck struct device *
930d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name,
931d560168bSGuenter Roeck 				     void *drvdata,
932d560168bSGuenter Roeck 				     const struct hwmon_chip_info *chip,
933d560168bSGuenter Roeck 				     const struct attribute_group **groups)
934d560168bSGuenter Roeck {
935d560168bSGuenter Roeck 	struct device **ptr, *hwdev;
936d560168bSGuenter Roeck 
937d560168bSGuenter Roeck 	if (!dev)
938d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
939d560168bSGuenter Roeck 
940d560168bSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
941d560168bSGuenter Roeck 	if (!ptr)
942d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
943d560168bSGuenter Roeck 
944d560168bSGuenter Roeck 	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
945d560168bSGuenter Roeck 						groups);
946d560168bSGuenter Roeck 	if (IS_ERR(hwdev))
947d560168bSGuenter Roeck 		goto error;
948d560168bSGuenter Roeck 
949d560168bSGuenter Roeck 	*ptr = hwdev;
950d560168bSGuenter Roeck 	devres_add(dev, ptr);
951d560168bSGuenter Roeck 
952d560168bSGuenter Roeck 	return hwdev;
953d560168bSGuenter Roeck 
954d560168bSGuenter Roeck error:
955d560168bSGuenter Roeck 	devres_free(ptr);
956d560168bSGuenter Roeck 	return hwdev;
957d560168bSGuenter Roeck }
958d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
959d560168bSGuenter Roeck 
96074188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data)
96174188cbaSGuenter Roeck {
96274188cbaSGuenter Roeck 	struct device **hwdev = res;
96374188cbaSGuenter Roeck 
96474188cbaSGuenter Roeck 	return *hwdev == data;
96574188cbaSGuenter Roeck }
96674188cbaSGuenter Roeck 
96774188cbaSGuenter Roeck /**
96874188cbaSGuenter Roeck  * devm_hwmon_device_unregister - removes a previously registered hwmon device
96974188cbaSGuenter Roeck  *
97074188cbaSGuenter Roeck  * @dev: the parent device of the device to unregister
97174188cbaSGuenter Roeck  */
97274188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev)
97374188cbaSGuenter Roeck {
97474188cbaSGuenter Roeck 	WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
97574188cbaSGuenter Roeck }
97674188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
97774188cbaSGuenter Roeck 
9782958b1ecSJean Delvare static void __init hwmon_pci_quirks(void)
9792958b1ecSJean Delvare {
9802958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI
9812958b1ecSJean Delvare 	struct pci_dev *sb;
9822958b1ecSJean Delvare 	u16 base;
9832958b1ecSJean Delvare 	u8 enable;
9842958b1ecSJean Delvare 
9852958b1ecSJean Delvare 	/* Open access to 0x295-0x296 on MSI MS-7031 */
9862958b1ecSJean Delvare 	sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
987d6dab7ddSJean Delvare 	if (sb) {
988d6dab7ddSJean Delvare 		if (sb->subsystem_vendor == 0x1462 &&	/* MSI */
989d6dab7ddSJean Delvare 		    sb->subsystem_device == 0x0031) {	/* MS-7031 */
9902958b1ecSJean Delvare 			pci_read_config_byte(sb, 0x48, &enable);
9912958b1ecSJean Delvare 			pci_read_config_word(sb, 0x64, &base);
9922958b1ecSJean Delvare 
9932958b1ecSJean Delvare 			if (base == 0 && !(enable & BIT(2))) {
9942958b1ecSJean Delvare 				dev_info(&sb->dev,
9952958b1ecSJean Delvare 					 "Opening wide generic port at 0x295\n");
9962958b1ecSJean Delvare 				pci_write_config_word(sb, 0x64, 0x295);
997d6dab7ddSJean Delvare 				pci_write_config_byte(sb, 0x48,
998d6dab7ddSJean Delvare 						      enable | BIT(2));
9992958b1ecSJean Delvare 			}
10002958b1ecSJean Delvare 		}
1001d6dab7ddSJean Delvare 		pci_dev_put(sb);
1002d6dab7ddSJean Delvare 	}
10032958b1ecSJean Delvare #endif
10042958b1ecSJean Delvare }
10052958b1ecSJean Delvare 
10061236441fSMark M. Hoffman static int __init hwmon_init(void)
10071236441fSMark M. Hoffman {
1008bab2243cSGuenter Roeck 	int err;
1009bab2243cSGuenter Roeck 
10102958b1ecSJean Delvare 	hwmon_pci_quirks();
10112958b1ecSJean Delvare 
1012bab2243cSGuenter Roeck 	err = class_register(&hwmon_class);
1013bab2243cSGuenter Roeck 	if (err) {
1014bab2243cSGuenter Roeck 		pr_err("couldn't register hwmon sysfs class\n");
1015bab2243cSGuenter Roeck 		return err;
10161236441fSMark M. Hoffman 	}
10171236441fSMark M. Hoffman 	return 0;
10181236441fSMark M. Hoffman }
10191236441fSMark M. Hoffman 
10201236441fSMark M. Hoffman static void __exit hwmon_exit(void)
10211236441fSMark M. Hoffman {
1022bab2243cSGuenter Roeck 	class_unregister(&hwmon_class);
10231236441fSMark M. Hoffman }
10241236441fSMark M. Hoffman 
102537f54ee5SDavid Brownell subsys_initcall(hwmon_init);
10261236441fSMark M. Hoffman module_exit(hwmon_exit);
10271236441fSMark M. Hoffman 
10281236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
10291236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
10301236441fSMark M. Hoffman MODULE_LICENSE("GPL");
10311236441fSMark M. Hoffman 
1032