xref: /linux/drivers/hwmon/hwmon.c (revision 1b5f517cca36292076d9e38fa6e33a257703e62e)
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>
181597b374SGuenter 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;
351597b374SGuenter 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 {
611597b374SGuenter Roeck 	struct list_head node;		/* hwmon tzdata list entry */
623bf8bdcfSGuenter Roeck 	struct device *dev;		/* Reference to hwmon device */
63d560168bSGuenter Roeck 	int index;			/* sensor index */
641597b374SGuenter 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 {
8277d76768SYang Li 	struct device *dev = kobj_to_dev(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 
156a5f6c0f8SDmitry Osipenko static int hwmon_thermal_set_trips(void *data, int low, int high)
157a5f6c0f8SDmitry Osipenko {
158a5f6c0f8SDmitry Osipenko 	struct hwmon_thermal_data *tdata = data;
159a5f6c0f8SDmitry Osipenko 	struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
160a5f6c0f8SDmitry Osipenko 	const struct hwmon_chip_info *chip = hwdev->chip;
161a5f6c0f8SDmitry Osipenko 	const struct hwmon_channel_info **info = chip->info;
162a5f6c0f8SDmitry Osipenko 	unsigned int i;
163a5f6c0f8SDmitry Osipenko 	int err;
164a5f6c0f8SDmitry Osipenko 
165a5f6c0f8SDmitry Osipenko 	if (!chip->ops->write)
166a5f6c0f8SDmitry Osipenko 		return 0;
167a5f6c0f8SDmitry Osipenko 
168a5f6c0f8SDmitry Osipenko 	for (i = 0; info[i] && info[i]->type != hwmon_temp; i++)
169a5f6c0f8SDmitry Osipenko 		continue;
170a5f6c0f8SDmitry Osipenko 
171a5f6c0f8SDmitry Osipenko 	if (!info[i])
172a5f6c0f8SDmitry Osipenko 		return 0;
173a5f6c0f8SDmitry Osipenko 
174a5f6c0f8SDmitry Osipenko 	if (info[i]->config[tdata->index] & HWMON_T_MIN) {
175a5f6c0f8SDmitry Osipenko 		err = chip->ops->write(tdata->dev, hwmon_temp,
176a5f6c0f8SDmitry Osipenko 				       hwmon_temp_min, tdata->index, low);
177a5f6c0f8SDmitry Osipenko 		if (err && err != -EOPNOTSUPP)
178a5f6c0f8SDmitry Osipenko 			return err;
179a5f6c0f8SDmitry Osipenko 	}
180a5f6c0f8SDmitry Osipenko 
181a5f6c0f8SDmitry Osipenko 	if (info[i]->config[tdata->index] & HWMON_T_MAX) {
182a5f6c0f8SDmitry Osipenko 		err = chip->ops->write(tdata->dev, hwmon_temp,
183a5f6c0f8SDmitry Osipenko 				       hwmon_temp_max, tdata->index, high);
184a5f6c0f8SDmitry Osipenko 		if (err && err != -EOPNOTSUPP)
185a5f6c0f8SDmitry Osipenko 			return err;
186a5f6c0f8SDmitry Osipenko 	}
187a5f6c0f8SDmitry Osipenko 
188a5f6c0f8SDmitry Osipenko 	return 0;
189a5f6c0f8SDmitry Osipenko }
190a5f6c0f8SDmitry Osipenko 
191c9920650SJulia Lawall static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
192d560168bSGuenter Roeck 	.get_temp = hwmon_thermal_get_temp,
193a5f6c0f8SDmitry Osipenko 	.set_trips = hwmon_thermal_set_trips,
194d560168bSGuenter Roeck };
195d560168bSGuenter Roeck 
1961597b374SGuenter Roeck static void hwmon_thermal_remove_sensor(void *data)
1971597b374SGuenter Roeck {
1981597b374SGuenter Roeck 	list_del(data);
1991597b374SGuenter Roeck }
2001597b374SGuenter Roeck 
2013bf8bdcfSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, int index)
202d560168bSGuenter Roeck {
2031597b374SGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(dev);
204d560168bSGuenter Roeck 	struct hwmon_thermal_data *tdata;
20547c332deSLinus Walleij 	struct thermal_zone_device *tzd;
2061597b374SGuenter Roeck 	int err;
207d560168bSGuenter Roeck 
208d560168bSGuenter Roeck 	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
209d560168bSGuenter Roeck 	if (!tdata)
210d560168bSGuenter Roeck 		return -ENOMEM;
211d560168bSGuenter Roeck 
2123bf8bdcfSGuenter Roeck 	tdata->dev = dev;
213d560168bSGuenter Roeck 	tdata->index = index;
214d560168bSGuenter Roeck 
2153bf8bdcfSGuenter Roeck 	tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata,
216d560168bSGuenter Roeck 						   &hwmon_thermal_ops);
217*1b5f517cSGuenter Roeck 	if (IS_ERR(tzd)) {
218*1b5f517cSGuenter Roeck 		if (PTR_ERR(tzd) != -ENODEV)
21947c332deSLinus Walleij 			return PTR_ERR(tzd);
220*1b5f517cSGuenter Roeck 		dev_info(dev, "temp%d_input not attached to any thermal zone\n",
221*1b5f517cSGuenter Roeck 			 index + 1);
222*1b5f517cSGuenter Roeck 		devm_kfree(dev, tdata);
223*1b5f517cSGuenter Roeck 		return 0;
224*1b5f517cSGuenter Roeck 	}
225d560168bSGuenter Roeck 
2261597b374SGuenter Roeck 	err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node);
2271597b374SGuenter Roeck 	if (err)
2281597b374SGuenter Roeck 		return err;
2291597b374SGuenter Roeck 
2301597b374SGuenter Roeck 	tdata->tzd = tzd;
2311597b374SGuenter Roeck 	list_add(&tdata->node, &hwdev->tzdata);
2321597b374SGuenter Roeck 
233d560168bSGuenter Roeck 	return 0;
234d560168bSGuenter Roeck }
23544e3ad88SAkinobu Mita 
23644e3ad88SAkinobu Mita static int hwmon_thermal_register_sensors(struct device *dev)
23744e3ad88SAkinobu Mita {
23844e3ad88SAkinobu Mita 	struct hwmon_device *hwdev = to_hwmon_device(dev);
23944e3ad88SAkinobu Mita 	const struct hwmon_chip_info *chip = hwdev->chip;
24044e3ad88SAkinobu Mita 	const struct hwmon_channel_info **info = chip->info;
24144e3ad88SAkinobu Mita 	void *drvdata = dev_get_drvdata(dev);
24244e3ad88SAkinobu Mita 	int i;
24344e3ad88SAkinobu Mita 
24444e3ad88SAkinobu Mita 	for (i = 1; info[i]; i++) {
24544e3ad88SAkinobu Mita 		int j;
24644e3ad88SAkinobu Mita 
24744e3ad88SAkinobu Mita 		if (info[i]->type != hwmon_temp)
24844e3ad88SAkinobu Mita 			continue;
24944e3ad88SAkinobu Mita 
25044e3ad88SAkinobu Mita 		for (j = 0; info[i]->config[j]; j++) {
25144e3ad88SAkinobu Mita 			int err;
25244e3ad88SAkinobu Mita 
25344e3ad88SAkinobu Mita 			if (!(info[i]->config[j] & HWMON_T_INPUT) ||
25444e3ad88SAkinobu Mita 			    !chip->ops->is_visible(drvdata, hwmon_temp,
25544e3ad88SAkinobu Mita 						   hwmon_temp_input, j))
25644e3ad88SAkinobu Mita 				continue;
25744e3ad88SAkinobu Mita 
25844e3ad88SAkinobu Mita 			err = hwmon_thermal_add_sensor(dev, j);
25944e3ad88SAkinobu Mita 			if (err)
26044e3ad88SAkinobu Mita 				return err;
26144e3ad88SAkinobu Mita 		}
26244e3ad88SAkinobu Mita 	}
26344e3ad88SAkinobu Mita 
26444e3ad88SAkinobu Mita 	return 0;
26544e3ad88SAkinobu Mita }
26644e3ad88SAkinobu Mita 
2671597b374SGuenter Roeck static void hwmon_thermal_notify(struct device *dev, int index)
2681597b374SGuenter Roeck {
2691597b374SGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(dev);
2701597b374SGuenter Roeck 	struct hwmon_thermal_data *tzdata;
2711597b374SGuenter Roeck 
2721597b374SGuenter Roeck 	list_for_each_entry(tzdata, &hwdev->tzdata, node) {
2731597b374SGuenter Roeck 		if (tzdata->index == index) {
2741597b374SGuenter Roeck 			thermal_zone_device_update(tzdata->tzd,
2751597b374SGuenter Roeck 						   THERMAL_EVENT_UNSPECIFIED);
2761597b374SGuenter Roeck 		}
2771597b374SGuenter Roeck 	}
2781597b374SGuenter Roeck }
2791597b374SGuenter Roeck 
280d560168bSGuenter Roeck #else
28144e3ad88SAkinobu Mita static int hwmon_thermal_register_sensors(struct device *dev)
282d560168bSGuenter Roeck {
283d560168bSGuenter Roeck 	return 0;
284d560168bSGuenter Roeck }
2851597b374SGuenter Roeck 
2861597b374SGuenter Roeck static void hwmon_thermal_notify(struct device *dev, int index) { }
2871597b374SGuenter Roeck 
28886430c1aSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
289d560168bSGuenter Roeck 
29061b8ab2cSNicolin Chen static int hwmon_attr_base(enum hwmon_sensor_types type)
29161b8ab2cSNicolin Chen {
2924413405fSDr. David Alan Gilbert 	if (type == hwmon_in || type == hwmon_intrusion)
29361b8ab2cSNicolin Chen 		return 0;
29461b8ab2cSNicolin Chen 	return 1;
29561b8ab2cSNicolin Chen }
29661b8ab2cSNicolin Chen 
297d560168bSGuenter Roeck /* sysfs attribute management */
298d560168bSGuenter Roeck 
299d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev,
300d560168bSGuenter Roeck 			       struct device_attribute *devattr, char *buf)
301d560168bSGuenter Roeck {
302d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
303d560168bSGuenter Roeck 	long val;
304d560168bSGuenter Roeck 	int ret;
305d560168bSGuenter Roeck 
306d560168bSGuenter Roeck 	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
307d560168bSGuenter Roeck 			       &val);
308d560168bSGuenter Roeck 	if (ret < 0)
309d560168bSGuenter Roeck 		return ret;
310d560168bSGuenter Roeck 
31161b8ab2cSNicolin Chen 	trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
31261b8ab2cSNicolin Chen 			      hattr->name, val);
31361b8ab2cSNicolin Chen 
314d560168bSGuenter Roeck 	return sprintf(buf, "%ld\n", val);
315d560168bSGuenter Roeck }
316d560168bSGuenter Roeck 
317e159ab5cSGuenter Roeck static ssize_t hwmon_attr_show_string(struct device *dev,
318e159ab5cSGuenter Roeck 				      struct device_attribute *devattr,
319e159ab5cSGuenter Roeck 				      char *buf)
320e159ab5cSGuenter Roeck {
321e159ab5cSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
32261b8ab2cSNicolin Chen 	enum hwmon_sensor_types type = hattr->type;
3235ba6bcbcSJean Delvare 	const char *s;
324e159ab5cSGuenter Roeck 	int ret;
325e159ab5cSGuenter Roeck 
326e159ab5cSGuenter Roeck 	ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
327e159ab5cSGuenter Roeck 				      hattr->index, &s);
328e159ab5cSGuenter Roeck 	if (ret < 0)
329e159ab5cSGuenter Roeck 		return ret;
330e159ab5cSGuenter Roeck 
33161b8ab2cSNicolin Chen 	trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
33261b8ab2cSNicolin Chen 				     hattr->name, s);
33361b8ab2cSNicolin Chen 
334e159ab5cSGuenter Roeck 	return sprintf(buf, "%s\n", s);
335e159ab5cSGuenter Roeck }
336e159ab5cSGuenter Roeck 
337d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev,
338d560168bSGuenter Roeck 				struct device_attribute *devattr,
339d560168bSGuenter Roeck 				const char *buf, size_t count)
340d560168bSGuenter Roeck {
341d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
342d560168bSGuenter Roeck 	long val;
343d560168bSGuenter Roeck 	int ret;
344d560168bSGuenter Roeck 
345d560168bSGuenter Roeck 	ret = kstrtol(buf, 10, &val);
346d560168bSGuenter Roeck 	if (ret < 0)
347d560168bSGuenter Roeck 		return ret;
348d560168bSGuenter Roeck 
349d560168bSGuenter Roeck 	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
350d560168bSGuenter Roeck 				val);
351d560168bSGuenter Roeck 	if (ret < 0)
352d560168bSGuenter Roeck 		return ret;
353d560168bSGuenter Roeck 
35461b8ab2cSNicolin Chen 	trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
35561b8ab2cSNicolin Chen 			       hattr->name, val);
356d560168bSGuenter Roeck 
35761b8ab2cSNicolin Chen 	return count;
358d560168bSGuenter Roeck }
359d560168bSGuenter Roeck 
360e159ab5cSGuenter Roeck static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
361e159ab5cSGuenter Roeck {
362e159ab5cSGuenter Roeck 	return (type == hwmon_temp && attr == hwmon_temp_label) ||
363e159ab5cSGuenter Roeck 	       (type == hwmon_in && attr == hwmon_in_label) ||
364e159ab5cSGuenter Roeck 	       (type == hwmon_curr && attr == hwmon_curr_label) ||
365e159ab5cSGuenter Roeck 	       (type == hwmon_power && attr == hwmon_power_label) ||
366e159ab5cSGuenter Roeck 	       (type == hwmon_energy && attr == hwmon_energy_label) ||
367e159ab5cSGuenter Roeck 	       (type == hwmon_humidity && attr == hwmon_humidity_label) ||
368e159ab5cSGuenter Roeck 	       (type == hwmon_fan && attr == hwmon_fan_label);
369e159ab5cSGuenter Roeck }
370e159ab5cSGuenter Roeck 
3713bf8bdcfSGuenter Roeck static struct attribute *hwmon_genattr(const void *drvdata,
372d560168bSGuenter Roeck 				       enum hwmon_sensor_types type,
373d560168bSGuenter Roeck 				       u32 attr,
374d560168bSGuenter Roeck 				       int index,
375d560168bSGuenter Roeck 				       const char *template,
376d560168bSGuenter Roeck 				       const struct hwmon_ops *ops)
377d560168bSGuenter Roeck {
378d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr;
379d560168bSGuenter Roeck 	struct device_attribute *dattr;
380d560168bSGuenter Roeck 	struct attribute *a;
381d560168bSGuenter Roeck 	umode_t mode;
3823b443defSRasmus Villemoes 	const char *name;
383e159ab5cSGuenter Roeck 	bool is_string = is_string_attr(type, attr);
384d560168bSGuenter Roeck 
385d560168bSGuenter Roeck 	/* The attribute is invisible if there is no template string */
386d560168bSGuenter Roeck 	if (!template)
387d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
388d560168bSGuenter Roeck 
389d560168bSGuenter Roeck 	mode = ops->is_visible(drvdata, type, attr, index);
390d560168bSGuenter Roeck 	if (!mode)
391d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
392d560168bSGuenter Roeck 
3930d87116fSGuenter Roeck 	if ((mode & 0444) && ((is_string && !ops->read_string) ||
394e159ab5cSGuenter Roeck 				 (!is_string && !ops->read)))
395d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
3960d87116fSGuenter Roeck 	if ((mode & 0222) && !ops->write)
397d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
398d560168bSGuenter Roeck 
3993bf8bdcfSGuenter Roeck 	hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
400d560168bSGuenter Roeck 	if (!hattr)
401d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
402d560168bSGuenter Roeck 
4033a412d5eSGuenter Roeck 	if (type == hwmon_chip) {
4043b443defSRasmus Villemoes 		name = template;
4053a412d5eSGuenter Roeck 	} else {
4063a412d5eSGuenter Roeck 		scnprintf(hattr->name, sizeof(hattr->name), template,
4073a412d5eSGuenter Roeck 			  index + hwmon_attr_base(type));
4083a412d5eSGuenter Roeck 		name = hattr->name;
4093a412d5eSGuenter Roeck 	}
4103a412d5eSGuenter Roeck 
411d560168bSGuenter Roeck 	hattr->type = type;
412d560168bSGuenter Roeck 	hattr->attr = attr;
413d560168bSGuenter Roeck 	hattr->index = index;
414d560168bSGuenter Roeck 	hattr->ops = ops;
415d560168bSGuenter Roeck 
416d560168bSGuenter Roeck 	dattr = &hattr->dev_attr;
417e159ab5cSGuenter Roeck 	dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
418d560168bSGuenter Roeck 	dattr->store = hwmon_attr_store;
419d560168bSGuenter Roeck 
420d560168bSGuenter Roeck 	a = &dattr->attr;
421d560168bSGuenter Roeck 	sysfs_attr_init(a);
422d560168bSGuenter Roeck 	a->name = name;
423d560168bSGuenter Roeck 	a->mode = mode;
424d560168bSGuenter Roeck 
425d560168bSGuenter Roeck 	return a;
426d560168bSGuenter Roeck }
427d560168bSGuenter Roeck 
428f4d325d5SGuenter Roeck /*
429f4d325d5SGuenter Roeck  * Chip attributes are not attribute templates but actual sysfs attributes.
430f4d325d5SGuenter Roeck  * See hwmon_genattr() for special handling.
431f4d325d5SGuenter Roeck  */
432f4d325d5SGuenter Roeck static const char * const hwmon_chip_attrs[] = {
433d560168bSGuenter Roeck 	[hwmon_chip_temp_reset_history] = "temp_reset_history",
43400d616cfSGuenter Roeck 	[hwmon_chip_in_reset_history] = "in_reset_history",
4359b26947cSGuenter Roeck 	[hwmon_chip_curr_reset_history] = "curr_reset_history",
436b308f5c7SGuenter Roeck 	[hwmon_chip_power_reset_history] = "power_reset_history",
437d560168bSGuenter Roeck 	[hwmon_chip_update_interval] = "update_interval",
438d560168bSGuenter Roeck 	[hwmon_chip_alarms] = "alarms",
4399f00995eSGuenter Roeck 	[hwmon_chip_samples] = "samples",
4409f00995eSGuenter Roeck 	[hwmon_chip_curr_samples] = "curr_samples",
4419f00995eSGuenter Roeck 	[hwmon_chip_in_samples] = "in_samples",
4429f00995eSGuenter Roeck 	[hwmon_chip_power_samples] = "power_samples",
4439f00995eSGuenter Roeck 	[hwmon_chip_temp_samples] = "temp_samples",
444d560168bSGuenter Roeck };
445d560168bSGuenter Roeck 
446d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = {
447002c6b54SGuenter Roeck 	[hwmon_temp_enable] = "temp%d_enable",
448d560168bSGuenter Roeck 	[hwmon_temp_input] = "temp%d_input",
449d560168bSGuenter Roeck 	[hwmon_temp_type] = "temp%d_type",
450d560168bSGuenter Roeck 	[hwmon_temp_lcrit] = "temp%d_lcrit",
451d560168bSGuenter Roeck 	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
452d560168bSGuenter Roeck 	[hwmon_temp_min] = "temp%d_min",
453d560168bSGuenter Roeck 	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
454d560168bSGuenter Roeck 	[hwmon_temp_max] = "temp%d_max",
455d560168bSGuenter Roeck 	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
456d560168bSGuenter Roeck 	[hwmon_temp_crit] = "temp%d_crit",
457d560168bSGuenter Roeck 	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
458d560168bSGuenter Roeck 	[hwmon_temp_emergency] = "temp%d_emergency",
459d560168bSGuenter Roeck 	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
460d560168bSGuenter Roeck 	[hwmon_temp_alarm] = "temp%d_alarm",
461d560168bSGuenter Roeck 	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
462d560168bSGuenter Roeck 	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
463d560168bSGuenter Roeck 	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
464d560168bSGuenter Roeck 	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
465d560168bSGuenter Roeck 	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
466d560168bSGuenter Roeck 	[hwmon_temp_fault] = "temp%d_fault",
467d560168bSGuenter Roeck 	[hwmon_temp_offset] = "temp%d_offset",
468d560168bSGuenter Roeck 	[hwmon_temp_label] = "temp%d_label",
469d560168bSGuenter Roeck 	[hwmon_temp_lowest] = "temp%d_lowest",
470d560168bSGuenter Roeck 	[hwmon_temp_highest] = "temp%d_highest",
471d560168bSGuenter Roeck 	[hwmon_temp_reset_history] = "temp%d_reset_history",
4721967f712SZbigniew Lukwinski 	[hwmon_temp_rated_min] = "temp%d_rated_min",
4731967f712SZbigniew Lukwinski 	[hwmon_temp_rated_max] = "temp%d_rated_max",
474d560168bSGuenter Roeck };
475d560168bSGuenter Roeck 
47600d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = {
477002c6b54SGuenter Roeck 	[hwmon_in_enable] = "in%d_enable",
47800d616cfSGuenter Roeck 	[hwmon_in_input] = "in%d_input",
47900d616cfSGuenter Roeck 	[hwmon_in_min] = "in%d_min",
48000d616cfSGuenter Roeck 	[hwmon_in_max] = "in%d_max",
48100d616cfSGuenter Roeck 	[hwmon_in_lcrit] = "in%d_lcrit",
48200d616cfSGuenter Roeck 	[hwmon_in_crit] = "in%d_crit",
48300d616cfSGuenter Roeck 	[hwmon_in_average] = "in%d_average",
48400d616cfSGuenter Roeck 	[hwmon_in_lowest] = "in%d_lowest",
48500d616cfSGuenter Roeck 	[hwmon_in_highest] = "in%d_highest",
48600d616cfSGuenter Roeck 	[hwmon_in_reset_history] = "in%d_reset_history",
48700d616cfSGuenter Roeck 	[hwmon_in_label] = "in%d_label",
48800d616cfSGuenter Roeck 	[hwmon_in_alarm] = "in%d_alarm",
48900d616cfSGuenter Roeck 	[hwmon_in_min_alarm] = "in%d_min_alarm",
49000d616cfSGuenter Roeck 	[hwmon_in_max_alarm] = "in%d_max_alarm",
49100d616cfSGuenter Roeck 	[hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
49200d616cfSGuenter Roeck 	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
4931967f712SZbigniew Lukwinski 	[hwmon_in_rated_min] = "in%d_rated_min",
4941967f712SZbigniew Lukwinski 	[hwmon_in_rated_max] = "in%d_rated_max",
49500d616cfSGuenter Roeck };
49600d616cfSGuenter Roeck 
4979b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = {
498002c6b54SGuenter Roeck 	[hwmon_curr_enable] = "curr%d_enable",
4999b26947cSGuenter Roeck 	[hwmon_curr_input] = "curr%d_input",
5009b26947cSGuenter Roeck 	[hwmon_curr_min] = "curr%d_min",
5019b26947cSGuenter Roeck 	[hwmon_curr_max] = "curr%d_max",
5029b26947cSGuenter Roeck 	[hwmon_curr_lcrit] = "curr%d_lcrit",
5039b26947cSGuenter Roeck 	[hwmon_curr_crit] = "curr%d_crit",
5049b26947cSGuenter Roeck 	[hwmon_curr_average] = "curr%d_average",
5059b26947cSGuenter Roeck 	[hwmon_curr_lowest] = "curr%d_lowest",
5069b26947cSGuenter Roeck 	[hwmon_curr_highest] = "curr%d_highest",
5079b26947cSGuenter Roeck 	[hwmon_curr_reset_history] = "curr%d_reset_history",
5089b26947cSGuenter Roeck 	[hwmon_curr_label] = "curr%d_label",
5099b26947cSGuenter Roeck 	[hwmon_curr_alarm] = "curr%d_alarm",
5109b26947cSGuenter Roeck 	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
5119b26947cSGuenter Roeck 	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
5129b26947cSGuenter Roeck 	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
5139b26947cSGuenter Roeck 	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
5141967f712SZbigniew Lukwinski 	[hwmon_curr_rated_min] = "curr%d_rated_min",
5151967f712SZbigniew Lukwinski 	[hwmon_curr_rated_max] = "curr%d_rated_max",
5169b26947cSGuenter Roeck };
5179b26947cSGuenter Roeck 
518b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = {
519002c6b54SGuenter Roeck 	[hwmon_power_enable] = "power%d_enable",
520b308f5c7SGuenter Roeck 	[hwmon_power_average] = "power%d_average",
521b308f5c7SGuenter Roeck 	[hwmon_power_average_interval] = "power%d_average_interval",
522b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_max] = "power%d_interval_max",
523b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_min] = "power%d_interval_min",
524b308f5c7SGuenter Roeck 	[hwmon_power_average_highest] = "power%d_average_highest",
525b308f5c7SGuenter Roeck 	[hwmon_power_average_lowest] = "power%d_average_lowest",
526b308f5c7SGuenter Roeck 	[hwmon_power_average_max] = "power%d_average_max",
527b308f5c7SGuenter Roeck 	[hwmon_power_average_min] = "power%d_average_min",
528b308f5c7SGuenter Roeck 	[hwmon_power_input] = "power%d_input",
529b308f5c7SGuenter Roeck 	[hwmon_power_input_highest] = "power%d_input_highest",
530b308f5c7SGuenter Roeck 	[hwmon_power_input_lowest] = "power%d_input_lowest",
531b308f5c7SGuenter Roeck 	[hwmon_power_reset_history] = "power%d_reset_history",
532b308f5c7SGuenter Roeck 	[hwmon_power_accuracy] = "power%d_accuracy",
533b308f5c7SGuenter Roeck 	[hwmon_power_cap] = "power%d_cap",
534b308f5c7SGuenter Roeck 	[hwmon_power_cap_hyst] = "power%d_cap_hyst",
535b308f5c7SGuenter Roeck 	[hwmon_power_cap_max] = "power%d_cap_max",
536b308f5c7SGuenter Roeck 	[hwmon_power_cap_min] = "power%d_cap_min",
537aa7f29b0SAndrew Lunn 	[hwmon_power_min] = "power%d_min",
538b308f5c7SGuenter Roeck 	[hwmon_power_max] = "power%d_max",
539aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit] = "power%d_lcrit",
540b308f5c7SGuenter Roeck 	[hwmon_power_crit] = "power%d_crit",
541b308f5c7SGuenter Roeck 	[hwmon_power_label] = "power%d_label",
542b308f5c7SGuenter Roeck 	[hwmon_power_alarm] = "power%d_alarm",
543b308f5c7SGuenter Roeck 	[hwmon_power_cap_alarm] = "power%d_cap_alarm",
544aa7f29b0SAndrew Lunn 	[hwmon_power_min_alarm] = "power%d_min_alarm",
545b308f5c7SGuenter Roeck 	[hwmon_power_max_alarm] = "power%d_max_alarm",
546aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
547b308f5c7SGuenter Roeck 	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
5481967f712SZbigniew Lukwinski 	[hwmon_power_rated_min] = "power%d_rated_min",
5491967f712SZbigniew Lukwinski 	[hwmon_power_rated_max] = "power%d_rated_max",
550b308f5c7SGuenter Roeck };
551b308f5c7SGuenter Roeck 
5526bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = {
553002c6b54SGuenter Roeck 	[hwmon_energy_enable] = "energy%d_enable",
5546bfcca44SGuenter Roeck 	[hwmon_energy_input] = "energy%d_input",
5556bfcca44SGuenter Roeck 	[hwmon_energy_label] = "energy%d_label",
5566bfcca44SGuenter Roeck };
5576bfcca44SGuenter Roeck 
5586bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = {
559002c6b54SGuenter Roeck 	[hwmon_humidity_enable] = "humidity%d_enable",
5606bfcca44SGuenter Roeck 	[hwmon_humidity_input] = "humidity%d_input",
5616bfcca44SGuenter Roeck 	[hwmon_humidity_label] = "humidity%d_label",
5626bfcca44SGuenter Roeck 	[hwmon_humidity_min] = "humidity%d_min",
5636bfcca44SGuenter Roeck 	[hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
5646bfcca44SGuenter Roeck 	[hwmon_humidity_max] = "humidity%d_max",
5656bfcca44SGuenter Roeck 	[hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
5666bfcca44SGuenter Roeck 	[hwmon_humidity_alarm] = "humidity%d_alarm",
5676bfcca44SGuenter Roeck 	[hwmon_humidity_fault] = "humidity%d_fault",
5681967f712SZbigniew Lukwinski 	[hwmon_humidity_rated_min] = "humidity%d_rated_min",
5691967f712SZbigniew Lukwinski 	[hwmon_humidity_rated_max] = "humidity%d_rated_max",
5706bfcca44SGuenter Roeck };
5716bfcca44SGuenter Roeck 
5728faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = {
573002c6b54SGuenter Roeck 	[hwmon_fan_enable] = "fan%d_enable",
5748faee73fSGuenter Roeck 	[hwmon_fan_input] = "fan%d_input",
5758faee73fSGuenter Roeck 	[hwmon_fan_label] = "fan%d_label",
5768faee73fSGuenter Roeck 	[hwmon_fan_min] = "fan%d_min",
5778faee73fSGuenter Roeck 	[hwmon_fan_max] = "fan%d_max",
5788faee73fSGuenter Roeck 	[hwmon_fan_div] = "fan%d_div",
5798faee73fSGuenter Roeck 	[hwmon_fan_pulses] = "fan%d_pulses",
5808faee73fSGuenter Roeck 	[hwmon_fan_target] = "fan%d_target",
5818faee73fSGuenter Roeck 	[hwmon_fan_alarm] = "fan%d_alarm",
5828faee73fSGuenter Roeck 	[hwmon_fan_min_alarm] = "fan%d_min_alarm",
5838faee73fSGuenter Roeck 	[hwmon_fan_max_alarm] = "fan%d_max_alarm",
5848faee73fSGuenter Roeck 	[hwmon_fan_fault] = "fan%d_fault",
5858faee73fSGuenter Roeck };
5868faee73fSGuenter Roeck 
587f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = {
588f9f7bb3aSGuenter Roeck 	[hwmon_pwm_input] = "pwm%d",
589f9f7bb3aSGuenter Roeck 	[hwmon_pwm_enable] = "pwm%d_enable",
590f9f7bb3aSGuenter Roeck 	[hwmon_pwm_mode] = "pwm%d_mode",
591f9f7bb3aSGuenter Roeck 	[hwmon_pwm_freq] = "pwm%d_freq",
592f9f7bb3aSGuenter Roeck };
593f9f7bb3aSGuenter Roeck 
5944413405fSDr. David Alan Gilbert static const char * const hwmon_intrusion_attr_templates[] = {
5954413405fSDr. David Alan Gilbert 	[hwmon_intrusion_alarm] = "intrusion%d_alarm",
5964413405fSDr. David Alan Gilbert 	[hwmon_intrusion_beep]  = "intrusion%d_beep",
5974413405fSDr. David Alan Gilbert };
5984413405fSDr. David Alan Gilbert 
599d560168bSGuenter Roeck static const char * const *__templates[] = {
600f4d325d5SGuenter Roeck 	[hwmon_chip] = hwmon_chip_attrs,
601d560168bSGuenter Roeck 	[hwmon_temp] = hwmon_temp_attr_templates,
60200d616cfSGuenter Roeck 	[hwmon_in] = hwmon_in_attr_templates,
6039b26947cSGuenter Roeck 	[hwmon_curr] = hwmon_curr_attr_templates,
604b308f5c7SGuenter Roeck 	[hwmon_power] = hwmon_power_attr_templates,
6056bfcca44SGuenter Roeck 	[hwmon_energy] = hwmon_energy_attr_templates,
6066bfcca44SGuenter Roeck 	[hwmon_humidity] = hwmon_humidity_attr_templates,
6078faee73fSGuenter Roeck 	[hwmon_fan] = hwmon_fan_attr_templates,
608f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = hwmon_pwm_attr_templates,
6094413405fSDr. David Alan Gilbert 	[hwmon_intrusion] = hwmon_intrusion_attr_templates,
610d560168bSGuenter Roeck };
611d560168bSGuenter Roeck 
612d560168bSGuenter Roeck static const int __templates_size[] = {
613f4d325d5SGuenter Roeck 	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
614d560168bSGuenter Roeck 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
61500d616cfSGuenter Roeck 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
6169b26947cSGuenter Roeck 	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
617b308f5c7SGuenter Roeck 	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
6186bfcca44SGuenter Roeck 	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
6196bfcca44SGuenter Roeck 	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
6208faee73fSGuenter Roeck 	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
621f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
6224413405fSDr. David Alan Gilbert 	[hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
623d560168bSGuenter Roeck };
624d560168bSGuenter Roeck 
6251597b374SGuenter Roeck int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
6261597b374SGuenter Roeck 		       u32 attr, int channel)
6271597b374SGuenter Roeck {
6281597b374SGuenter Roeck 	char sattr[MAX_SYSFS_ATTR_NAME_LENGTH];
6291597b374SGuenter Roeck 	const char * const *templates;
6301597b374SGuenter Roeck 	const char *template;
6311597b374SGuenter Roeck 	int base;
6321597b374SGuenter Roeck 
6331597b374SGuenter Roeck 	if (type >= ARRAY_SIZE(__templates))
6341597b374SGuenter Roeck 		return -EINVAL;
6351597b374SGuenter Roeck 	if (attr >= __templates_size[type])
6361597b374SGuenter Roeck 		return -EINVAL;
6371597b374SGuenter Roeck 
6381597b374SGuenter Roeck 	templates = __templates[type];
6391597b374SGuenter Roeck 	template = templates[attr];
6401597b374SGuenter Roeck 
6411597b374SGuenter Roeck 	base = hwmon_attr_base(type);
6421597b374SGuenter Roeck 
6431597b374SGuenter Roeck 	scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel);
6441597b374SGuenter Roeck 	sysfs_notify(&dev->kobj, NULL, sattr);
6451597b374SGuenter Roeck 	kobject_uevent(&dev->kobj, KOBJ_CHANGE);
6461597b374SGuenter Roeck 
6471597b374SGuenter Roeck 	if (type == hwmon_temp)
6481597b374SGuenter Roeck 		hwmon_thermal_notify(dev, channel);
6491597b374SGuenter Roeck 
6501597b374SGuenter Roeck 	return 0;
6511597b374SGuenter Roeck }
6521597b374SGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_notify_event);
6531597b374SGuenter Roeck 
654d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
655d560168bSGuenter Roeck {
656d560168bSGuenter Roeck 	int i, n;
657d560168bSGuenter Roeck 
658d560168bSGuenter Roeck 	for (i = n = 0; info->config[i]; i++)
659d560168bSGuenter Roeck 		n += hweight32(info->config[i]);
660d560168bSGuenter Roeck 
661d560168bSGuenter Roeck 	return n;
662d560168bSGuenter Roeck }
663d560168bSGuenter Roeck 
6643bf8bdcfSGuenter Roeck static int hwmon_genattrs(const void *drvdata,
665d560168bSGuenter Roeck 			  struct attribute **attrs,
666d560168bSGuenter Roeck 			  const struct hwmon_ops *ops,
667d560168bSGuenter Roeck 			  const struct hwmon_channel_info *info)
668d560168bSGuenter Roeck {
669d560168bSGuenter Roeck 	const char * const *templates;
670d560168bSGuenter Roeck 	int template_size;
671d560168bSGuenter Roeck 	int i, aindex = 0;
672d560168bSGuenter Roeck 
673d560168bSGuenter Roeck 	if (info->type >= ARRAY_SIZE(__templates))
674d560168bSGuenter Roeck 		return -EINVAL;
675d560168bSGuenter Roeck 
676d560168bSGuenter Roeck 	templates = __templates[info->type];
677d560168bSGuenter Roeck 	template_size = __templates_size[info->type];
678d560168bSGuenter Roeck 
679d560168bSGuenter Roeck 	for (i = 0; info->config[i]; i++) {
680d560168bSGuenter Roeck 		u32 attr_mask = info->config[i];
681d560168bSGuenter Roeck 		u32 attr;
682d560168bSGuenter Roeck 
683d560168bSGuenter Roeck 		while (attr_mask) {
684d560168bSGuenter Roeck 			struct attribute *a;
685d560168bSGuenter Roeck 
686d560168bSGuenter Roeck 			attr = __ffs(attr_mask);
687d560168bSGuenter Roeck 			attr_mask &= ~BIT(attr);
688d560168bSGuenter Roeck 			if (attr >= template_size)
689d560168bSGuenter Roeck 				return -EINVAL;
6903bf8bdcfSGuenter Roeck 			a = hwmon_genattr(drvdata, info->type, attr, i,
691d560168bSGuenter Roeck 					  templates[attr], ops);
692d560168bSGuenter Roeck 			if (IS_ERR(a)) {
693d560168bSGuenter Roeck 				if (PTR_ERR(a) != -ENOENT)
694d560168bSGuenter Roeck 					return PTR_ERR(a);
695d560168bSGuenter Roeck 				continue;
696d560168bSGuenter Roeck 			}
697d560168bSGuenter Roeck 			attrs[aindex++] = a;
698d560168bSGuenter Roeck 		}
699d560168bSGuenter Roeck 	}
700d560168bSGuenter Roeck 	return aindex;
701d560168bSGuenter Roeck }
702d560168bSGuenter Roeck 
703d560168bSGuenter Roeck static struct attribute **
7043bf8bdcfSGuenter Roeck __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
705d560168bSGuenter Roeck {
706d560168bSGuenter Roeck 	int ret, i, aindex = 0, nattrs = 0;
707d560168bSGuenter Roeck 	struct attribute **attrs;
708d560168bSGuenter Roeck 
709d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++)
710d560168bSGuenter Roeck 		nattrs += hwmon_num_channel_attrs(chip->info[i]);
711d560168bSGuenter Roeck 
712d560168bSGuenter Roeck 	if (nattrs == 0)
713d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
714d560168bSGuenter Roeck 
7153bf8bdcfSGuenter Roeck 	attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
716d560168bSGuenter Roeck 	if (!attrs)
717d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
718d560168bSGuenter Roeck 
719d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++) {
7203bf8bdcfSGuenter Roeck 		ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
721d560168bSGuenter Roeck 				     chip->info[i]);
7223bf8bdcfSGuenter Roeck 		if (ret < 0) {
7233bf8bdcfSGuenter Roeck 			hwmon_free_attrs(attrs);
724d560168bSGuenter Roeck 			return ERR_PTR(ret);
7253bf8bdcfSGuenter Roeck 		}
726d560168bSGuenter Roeck 		aindex += ret;
727d560168bSGuenter Roeck 	}
728d560168bSGuenter Roeck 
729d560168bSGuenter Roeck 	return attrs;
730d560168bSGuenter Roeck }
731d560168bSGuenter Roeck 
732d560168bSGuenter Roeck static struct device *
733d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
734d560168bSGuenter Roeck 			const struct hwmon_chip_info *chip,
735d560168bSGuenter Roeck 			const struct attribute_group **groups)
736d560168bSGuenter Roeck {
737d560168bSGuenter Roeck 	struct hwmon_device *hwdev;
738d560168bSGuenter Roeck 	struct device *hdev;
73944e3ad88SAkinobu Mita 	int i, err, id;
740d560168bSGuenter Roeck 
74174d3b641SGuenter Roeck 	/* Complain about invalid characters in hwmon name attribute */
742d560168bSGuenter Roeck 	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
74374d3b641SGuenter Roeck 		dev_warn(dev,
74474d3b641SGuenter Roeck 			 "hwmon: '%s' is not a valid name attribute, please fix\n",
74574d3b641SGuenter Roeck 			 name);
746d560168bSGuenter Roeck 
747d560168bSGuenter Roeck 	id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
748d560168bSGuenter Roeck 	if (id < 0)
749d560168bSGuenter Roeck 		return ERR_PTR(id);
750d560168bSGuenter Roeck 
751d560168bSGuenter Roeck 	hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
752d560168bSGuenter Roeck 	if (hwdev == NULL) {
753d560168bSGuenter Roeck 		err = -ENOMEM;
754d560168bSGuenter Roeck 		goto ida_remove;
755d560168bSGuenter Roeck 	}
756d560168bSGuenter Roeck 
757d560168bSGuenter Roeck 	hdev = &hwdev->dev;
758d560168bSGuenter Roeck 
759239552f4SGuenter Roeck 	if (chip) {
760d560168bSGuenter Roeck 		struct attribute **attrs;
761b2a4cc3aSGuenter Roeck 		int ngroups = 2; /* terminating NULL plus &hwdev->groups */
762d560168bSGuenter Roeck 
763d560168bSGuenter Roeck 		if (groups)
764d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
765d560168bSGuenter Roeck 				ngroups++;
766d560168bSGuenter Roeck 
7673bf8bdcfSGuenter Roeck 		hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
76838d8ed65SColin Ian King 		if (!hwdev->groups) {
76938d8ed65SColin Ian King 			err = -ENOMEM;
77038d8ed65SColin Ian King 			goto free_hwmon;
77138d8ed65SColin Ian King 		}
772d560168bSGuenter Roeck 
7733bf8bdcfSGuenter Roeck 		attrs = __hwmon_create_attrs(drvdata, chip);
774d560168bSGuenter Roeck 		if (IS_ERR(attrs)) {
775d560168bSGuenter Roeck 			err = PTR_ERR(attrs);
776d560168bSGuenter Roeck 			goto free_hwmon;
777d560168bSGuenter Roeck 		}
778d560168bSGuenter Roeck 
779d560168bSGuenter Roeck 		hwdev->group.attrs = attrs;
780d560168bSGuenter Roeck 		ngroups = 0;
781d560168bSGuenter Roeck 		hwdev->groups[ngroups++] = &hwdev->group;
782d560168bSGuenter Roeck 
783d560168bSGuenter Roeck 		if (groups) {
784d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
785d560168bSGuenter Roeck 				hwdev->groups[ngroups++] = groups[i];
786d560168bSGuenter Roeck 		}
787d560168bSGuenter Roeck 
788d560168bSGuenter Roeck 		hdev->groups = hwdev->groups;
789d560168bSGuenter Roeck 	} else {
790d560168bSGuenter Roeck 		hdev->groups = groups;
791d560168bSGuenter Roeck 	}
792d560168bSGuenter Roeck 
793d560168bSGuenter Roeck 	hwdev->name = name;
794d560168bSGuenter Roeck 	hdev->class = &hwmon_class;
795d560168bSGuenter Roeck 	hdev->parent = dev;
796d560168bSGuenter Roeck 	hdev->of_node = dev ? dev->of_node : NULL;
797d560168bSGuenter Roeck 	hwdev->chip = chip;
798d560168bSGuenter Roeck 	dev_set_drvdata(hdev, drvdata);
799d560168bSGuenter Roeck 	dev_set_name(hdev, HWMON_ID_FORMAT, id);
800d560168bSGuenter Roeck 	err = device_register(hdev);
801ada61aa0SYang Yingliang 	if (err) {
802ada61aa0SYang Yingliang 		put_device(hdev);
803ada61aa0SYang Yingliang 		goto ida_remove;
804ada61aa0SYang Yingliang 	}
805d560168bSGuenter Roeck 
8061597b374SGuenter Roeck 	INIT_LIST_HEAD(&hwdev->tzdata);
8071597b374SGuenter Roeck 
808c41dd48eSEduardo Valentin 	if (dev && dev->of_node && chip && chip->ops->read &&
809d560168bSGuenter Roeck 	    chip->info[0]->type == hwmon_chip &&
810d560168bSGuenter Roeck 	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
81144e3ad88SAkinobu Mita 		err = hwmon_thermal_register_sensors(hdev);
81274e35127SDmitry Osipenko 		if (err) {
81374e35127SDmitry Osipenko 			device_unregister(hdev);
814792eac18SGuenter Roeck 			/*
81544e3ad88SAkinobu Mita 			 * Don't worry about hwdev; hwmon_dev_release(), called
81644e3ad88SAkinobu Mita 			 * from device_unregister(), will free it.
817792eac18SGuenter Roeck 			 */
81874e35127SDmitry Osipenko 			goto ida_remove;
81974e35127SDmitry Osipenko 		}
82047c332deSLinus Walleij 	}
821d560168bSGuenter Roeck 
822d560168bSGuenter Roeck 	return hdev;
823d560168bSGuenter Roeck 
824d560168bSGuenter Roeck free_hwmon:
8253bf8bdcfSGuenter Roeck 	hwmon_dev_release(hdev);
826d560168bSGuenter Roeck ida_remove:
827d560168bSGuenter Roeck 	ida_simple_remove(&hwmon_ida, id);
828d560168bSGuenter Roeck 	return ERR_PTR(err);
829d560168bSGuenter Roeck }
830d560168bSGuenter Roeck 
8311236441fSMark M. Hoffman /**
832bab2243cSGuenter Roeck  * hwmon_device_register_with_groups - register w/ hwmon
833bab2243cSGuenter Roeck  * @dev: the parent device
834bab2243cSGuenter Roeck  * @name: hwmon name attribute
835bab2243cSGuenter Roeck  * @drvdata: driver data to attach to created device
836bab2243cSGuenter Roeck  * @groups: List of attribute groups to create
837bab2243cSGuenter Roeck  *
838bab2243cSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
839bab2243cSGuenter Roeck  * longer needed.
840bab2243cSGuenter Roeck  *
841bab2243cSGuenter Roeck  * Returns the pointer to the new device.
842bab2243cSGuenter Roeck  */
843bab2243cSGuenter Roeck struct device *
844bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name,
845bab2243cSGuenter Roeck 				  void *drvdata,
846bab2243cSGuenter Roeck 				  const struct attribute_group **groups)
847bab2243cSGuenter Roeck {
8488353863aSGuenter Roeck 	if (!name)
8498353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
8508353863aSGuenter Roeck 
851d560168bSGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
852bab2243cSGuenter Roeck }
853bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
854bab2243cSGuenter Roeck 
855bab2243cSGuenter Roeck /**
856d560168bSGuenter Roeck  * hwmon_device_register_with_info - register w/ hwmon
857d560168bSGuenter Roeck  * @dev: the parent device
858d560168bSGuenter Roeck  * @name: hwmon name attribute
859d560168bSGuenter Roeck  * @drvdata: driver data to attach to created device
8603870945aSGuenter Roeck  * @chip: pointer to hwmon chip information
861848ba0a2SGuenter Roeck  * @extra_groups: pointer to list of additional non-standard attribute groups
862d560168bSGuenter Roeck  *
863d560168bSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
864d560168bSGuenter Roeck  * longer needed.
865d560168bSGuenter Roeck  *
866d560168bSGuenter Roeck  * Returns the pointer to the new device.
867d560168bSGuenter Roeck  */
868d560168bSGuenter Roeck struct device *
869d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name,
870d560168bSGuenter Roeck 				void *drvdata,
871d560168bSGuenter Roeck 				const struct hwmon_chip_info *chip,
872848ba0a2SGuenter Roeck 				const struct attribute_group **extra_groups)
873d560168bSGuenter Roeck {
8748353863aSGuenter Roeck 	if (!name)
8758353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
8768353863aSGuenter Roeck 
877239552f4SGuenter Roeck 	if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
878d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
879d560168bSGuenter Roeck 
88059df4f4eSLucas Magasweran 	if (chip && !dev)
88159df4f4eSLucas Magasweran 		return ERR_PTR(-EINVAL);
88259df4f4eSLucas Magasweran 
883848ba0a2SGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
884d560168bSGuenter Roeck }
885d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
886d560168bSGuenter Roeck 
887d560168bSGuenter Roeck /**
8881beeffe4STony Jones  * hwmon_device_register - register w/ hwmon
8891236441fSMark M. Hoffman  * @dev: the device to register
8901236441fSMark M. Hoffman  *
8911beeffe4STony Jones  * hwmon_device_unregister() must be called when the device is no
8921236441fSMark M. Hoffman  * longer needed.
8931236441fSMark M. Hoffman  *
8941beeffe4STony Jones  * Returns the pointer to the new device.
8951236441fSMark M. Hoffman  */
8961beeffe4STony Jones struct device *hwmon_device_register(struct device *dev)
8971236441fSMark M. Hoffman {
898af1bd36cSGuenter Roeck 	dev_warn(dev,
899af1bd36cSGuenter Roeck 		 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
900af1bd36cSGuenter Roeck 
9018353863aSGuenter Roeck 	return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
9021236441fSMark M. Hoffman }
903839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register);
9041236441fSMark M. Hoffman 
9051236441fSMark M. Hoffman /**
9061236441fSMark M. Hoffman  * hwmon_device_unregister - removes the previously registered class device
9071236441fSMark M. Hoffman  *
9081beeffe4STony Jones  * @dev: the class device to destroy
9091236441fSMark M. Hoffman  */
9101beeffe4STony Jones void hwmon_device_unregister(struct device *dev)
9111236441fSMark M. Hoffman {
9121236441fSMark M. Hoffman 	int id;
9131236441fSMark M. Hoffman 
914739cf3a2SKay Sievers 	if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
9151beeffe4STony Jones 		device_unregister(dev);
9164ca5f468SJonathan Cameron 		ida_simple_remove(&hwmon_ida, id);
9171236441fSMark M. Hoffman 	} else
9181beeffe4STony Jones 		dev_dbg(dev->parent,
9191236441fSMark M. Hoffman 			"hwmon_device_unregister() failed: bad class ID!\n");
9201236441fSMark M. Hoffman }
921839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister);
9221236441fSMark M. Hoffman 
92374188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res)
92474188cbaSGuenter Roeck {
92574188cbaSGuenter Roeck 	struct device *hwdev = *(struct device **)res;
92674188cbaSGuenter Roeck 
92774188cbaSGuenter Roeck 	hwmon_device_unregister(hwdev);
92874188cbaSGuenter Roeck }
92974188cbaSGuenter Roeck 
93074188cbaSGuenter Roeck /**
93174188cbaSGuenter Roeck  * devm_hwmon_device_register_with_groups - register w/ hwmon
93274188cbaSGuenter Roeck  * @dev: the parent device
93374188cbaSGuenter Roeck  * @name: hwmon name attribute
93474188cbaSGuenter Roeck  * @drvdata: driver data to attach to created device
93574188cbaSGuenter Roeck  * @groups: List of attribute groups to create
93674188cbaSGuenter Roeck  *
93774188cbaSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
93874188cbaSGuenter Roeck  * unregistered with the parent device.
93974188cbaSGuenter Roeck  */
94074188cbaSGuenter Roeck struct device *
94174188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
94274188cbaSGuenter Roeck 				       void *drvdata,
94374188cbaSGuenter Roeck 				       const struct attribute_group **groups)
94474188cbaSGuenter Roeck {
94574188cbaSGuenter Roeck 	struct device **ptr, *hwdev;
94674188cbaSGuenter Roeck 
94774188cbaSGuenter Roeck 	if (!dev)
94874188cbaSGuenter Roeck 		return ERR_PTR(-EINVAL);
94974188cbaSGuenter Roeck 
95074188cbaSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
95174188cbaSGuenter Roeck 	if (!ptr)
95274188cbaSGuenter Roeck 		return ERR_PTR(-ENOMEM);
95374188cbaSGuenter Roeck 
95474188cbaSGuenter Roeck 	hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
95574188cbaSGuenter Roeck 	if (IS_ERR(hwdev))
95674188cbaSGuenter Roeck 		goto error;
95774188cbaSGuenter Roeck 
95874188cbaSGuenter Roeck 	*ptr = hwdev;
95974188cbaSGuenter Roeck 	devres_add(dev, ptr);
96074188cbaSGuenter Roeck 	return hwdev;
96174188cbaSGuenter Roeck 
96274188cbaSGuenter Roeck error:
96374188cbaSGuenter Roeck 	devres_free(ptr);
96474188cbaSGuenter Roeck 	return hwdev;
96574188cbaSGuenter Roeck }
96674188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
96774188cbaSGuenter Roeck 
968d560168bSGuenter Roeck /**
969d560168bSGuenter Roeck  * devm_hwmon_device_register_with_info - register w/ hwmon
970d560168bSGuenter Roeck  * @dev:	the parent device
971d560168bSGuenter Roeck  * @name:	hwmon name attribute
972d560168bSGuenter Roeck  * @drvdata:	driver data to attach to created device
9733870945aSGuenter Roeck  * @chip:	pointer to hwmon chip information
9743870945aSGuenter Roeck  * @groups:	pointer to list of driver specific attribute groups
975d560168bSGuenter Roeck  *
976d560168bSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
977d560168bSGuenter Roeck  * unregistered with the parent device.
978d560168bSGuenter Roeck  */
979d560168bSGuenter Roeck struct device *
980d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name,
981d560168bSGuenter Roeck 				     void *drvdata,
982d560168bSGuenter Roeck 				     const struct hwmon_chip_info *chip,
983d560168bSGuenter Roeck 				     const struct attribute_group **groups)
984d560168bSGuenter Roeck {
985d560168bSGuenter Roeck 	struct device **ptr, *hwdev;
986d560168bSGuenter Roeck 
987d560168bSGuenter Roeck 	if (!dev)
988d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
989d560168bSGuenter Roeck 
990d560168bSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
991d560168bSGuenter Roeck 	if (!ptr)
992d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
993d560168bSGuenter Roeck 
994d560168bSGuenter Roeck 	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
995d560168bSGuenter Roeck 						groups);
996d560168bSGuenter Roeck 	if (IS_ERR(hwdev))
997d560168bSGuenter Roeck 		goto error;
998d560168bSGuenter Roeck 
999d560168bSGuenter Roeck 	*ptr = hwdev;
1000d560168bSGuenter Roeck 	devres_add(dev, ptr);
1001d560168bSGuenter Roeck 
1002d560168bSGuenter Roeck 	return hwdev;
1003d560168bSGuenter Roeck 
1004d560168bSGuenter Roeck error:
1005d560168bSGuenter Roeck 	devres_free(ptr);
1006d560168bSGuenter Roeck 	return hwdev;
1007d560168bSGuenter Roeck }
1008d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
1009d560168bSGuenter Roeck 
101074188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data)
101174188cbaSGuenter Roeck {
101274188cbaSGuenter Roeck 	struct device **hwdev = res;
101374188cbaSGuenter Roeck 
101474188cbaSGuenter Roeck 	return *hwdev == data;
101574188cbaSGuenter Roeck }
101674188cbaSGuenter Roeck 
101774188cbaSGuenter Roeck /**
101874188cbaSGuenter Roeck  * devm_hwmon_device_unregister - removes a previously registered hwmon device
101974188cbaSGuenter Roeck  *
102074188cbaSGuenter Roeck  * @dev: the parent device of the device to unregister
102174188cbaSGuenter Roeck  */
102274188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev)
102374188cbaSGuenter Roeck {
102474188cbaSGuenter Roeck 	WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
102574188cbaSGuenter Roeck }
102674188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
102774188cbaSGuenter Roeck 
10282958b1ecSJean Delvare static void __init hwmon_pci_quirks(void)
10292958b1ecSJean Delvare {
10302958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI
10312958b1ecSJean Delvare 	struct pci_dev *sb;
10322958b1ecSJean Delvare 	u16 base;
10332958b1ecSJean Delvare 	u8 enable;
10342958b1ecSJean Delvare 
10352958b1ecSJean Delvare 	/* Open access to 0x295-0x296 on MSI MS-7031 */
10362958b1ecSJean Delvare 	sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
1037d6dab7ddSJean Delvare 	if (sb) {
1038d6dab7ddSJean Delvare 		if (sb->subsystem_vendor == 0x1462 &&	/* MSI */
1039d6dab7ddSJean Delvare 		    sb->subsystem_device == 0x0031) {	/* MS-7031 */
10402958b1ecSJean Delvare 			pci_read_config_byte(sb, 0x48, &enable);
10412958b1ecSJean Delvare 			pci_read_config_word(sb, 0x64, &base);
10422958b1ecSJean Delvare 
10432958b1ecSJean Delvare 			if (base == 0 && !(enable & BIT(2))) {
10442958b1ecSJean Delvare 				dev_info(&sb->dev,
10452958b1ecSJean Delvare 					 "Opening wide generic port at 0x295\n");
10462958b1ecSJean Delvare 				pci_write_config_word(sb, 0x64, 0x295);
1047d6dab7ddSJean Delvare 				pci_write_config_byte(sb, 0x48,
1048d6dab7ddSJean Delvare 						      enable | BIT(2));
10492958b1ecSJean Delvare 			}
10502958b1ecSJean Delvare 		}
1051d6dab7ddSJean Delvare 		pci_dev_put(sb);
1052d6dab7ddSJean Delvare 	}
10532958b1ecSJean Delvare #endif
10542958b1ecSJean Delvare }
10552958b1ecSJean Delvare 
10561236441fSMark M. Hoffman static int __init hwmon_init(void)
10571236441fSMark M. Hoffman {
1058bab2243cSGuenter Roeck 	int err;
1059bab2243cSGuenter Roeck 
10602958b1ecSJean Delvare 	hwmon_pci_quirks();
10612958b1ecSJean Delvare 
1062bab2243cSGuenter Roeck 	err = class_register(&hwmon_class);
1063bab2243cSGuenter Roeck 	if (err) {
1064bab2243cSGuenter Roeck 		pr_err("couldn't register hwmon sysfs class\n");
1065bab2243cSGuenter Roeck 		return err;
10661236441fSMark M. Hoffman 	}
10671236441fSMark M. Hoffman 	return 0;
10681236441fSMark M. Hoffman }
10691236441fSMark M. Hoffman 
10701236441fSMark M. Hoffman static void __exit hwmon_exit(void)
10711236441fSMark M. Hoffman {
1072bab2243cSGuenter Roeck 	class_unregister(&hwmon_class);
10731236441fSMark M. Hoffman }
10741236441fSMark M. Hoffman 
107537f54ee5SDavid Brownell subsys_initcall(hwmon_init);
10761236441fSMark M. Hoffman module_exit(hwmon_exit);
10771236441fSMark M. Hoffman 
10781236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
10791236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
10801236441fSMark M. Hoffman MODULE_LICENSE("GPL");
10811236441fSMark M. Hoffman 
1082