xref: /linux/drivers/hwmon/hwmon.c (revision 52115fc32905c0bacc244f1dace9a138518df0de)
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>
174ce6e8a8SGuenter Roeck #include <linux/i2c.h>
18c9ebbe6fSGuenter Roeck #include <linux/idr.h>
1925f98688SChristophe JAILLET #include <linux/kstrtox.h>
201597b374SGuenter Roeck #include <linux/list.h>
21c9ebbe6fSGuenter Roeck #include <linux/module.h>
222958b1ecSJean Delvare #include <linux/pci.h>
23e1c9d6d6SPaul Cercueil #include <linux/property.h>
24c9ebbe6fSGuenter Roeck #include <linux/slab.h>
25648cd48cSGuenter Roeck #include <linux/string.h>
26d560168bSGuenter Roeck #include <linux/thermal.h>
271236441fSMark M. Hoffman 
2861b8ab2cSNicolin Chen #define CREATE_TRACE_POINTS
2961b8ab2cSNicolin Chen #include <trace/events/hwmon.h>
3061b8ab2cSNicolin Chen 
311236441fSMark M. Hoffman #define HWMON_ID_PREFIX "hwmon"
321236441fSMark M. Hoffman #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
331236441fSMark M. Hoffman 
34bab2243cSGuenter Roeck struct hwmon_device {
35bab2243cSGuenter Roeck 	const char *name;
36e1c9d6d6SPaul Cercueil 	const char *label;
37bab2243cSGuenter Roeck 	struct device dev;
38d560168bSGuenter Roeck 	const struct hwmon_chip_info *chip;
391597b374SGuenter Roeck 	struct list_head tzdata;
40d560168bSGuenter Roeck 	struct attribute_group group;
41d560168bSGuenter Roeck 	const struct attribute_group **groups;
42bab2243cSGuenter Roeck };
43d560168bSGuenter Roeck 
44bab2243cSGuenter Roeck #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
45bab2243cSGuenter Roeck 
463a412d5eSGuenter Roeck #define MAX_SYSFS_ATTR_NAME_LENGTH	32
473a412d5eSGuenter Roeck 
48d560168bSGuenter Roeck struct hwmon_device_attribute {
49d560168bSGuenter Roeck 	struct device_attribute dev_attr;
50d560168bSGuenter Roeck 	const struct hwmon_ops *ops;
51d560168bSGuenter Roeck 	enum hwmon_sensor_types type;
52d560168bSGuenter Roeck 	u32 attr;
53d560168bSGuenter Roeck 	int index;
543a412d5eSGuenter Roeck 	char name[MAX_SYSFS_ATTR_NAME_LENGTH];
55d560168bSGuenter Roeck };
56d560168bSGuenter Roeck 
57d560168bSGuenter Roeck #define to_hwmon_attr(d) \
58d560168bSGuenter Roeck 	container_of(d, struct hwmon_device_attribute, dev_attr)
593bf8bdcfSGuenter Roeck #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
60d560168bSGuenter Roeck 
61d560168bSGuenter Roeck /*
62d560168bSGuenter Roeck  * Thermal zone information
63d560168bSGuenter Roeck  */
64d560168bSGuenter Roeck struct hwmon_thermal_data {
651597b374SGuenter Roeck 	struct list_head node;		/* hwmon tzdata list entry */
663bf8bdcfSGuenter Roeck 	struct device *dev;		/* Reference to hwmon device */
67d560168bSGuenter Roeck 	int index;			/* sensor index */
681597b374SGuenter Roeck 	struct thermal_zone_device *tzd;/* thermal zone device */
69d560168bSGuenter Roeck };
70d560168bSGuenter Roeck 
71bab2243cSGuenter Roeck static ssize_t
722ab0c6c5SJulia Lawall name_show(struct device *dev, struct device_attribute *attr, char *buf)
73bab2243cSGuenter Roeck {
74bab2243cSGuenter Roeck 	return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
75bab2243cSGuenter Roeck }
762ab0c6c5SJulia Lawall static DEVICE_ATTR_RO(name);
77bab2243cSGuenter Roeck 
78e1c9d6d6SPaul Cercueil static ssize_t
79e1c9d6d6SPaul Cercueil label_show(struct device *dev, struct device_attribute *attr, char *buf)
80e1c9d6d6SPaul Cercueil {
81e1c9d6d6SPaul Cercueil 	return sysfs_emit(buf, "%s\n", to_hwmon_device(dev)->label);
82e1c9d6d6SPaul Cercueil }
83e1c9d6d6SPaul Cercueil static DEVICE_ATTR_RO(label);
84e1c9d6d6SPaul Cercueil 
85bab2243cSGuenter Roeck static struct attribute *hwmon_dev_attrs[] = {
86bab2243cSGuenter Roeck 	&dev_attr_name.attr,
87e1c9d6d6SPaul Cercueil 	&dev_attr_label.attr,
88bab2243cSGuenter Roeck 	NULL
89bab2243cSGuenter Roeck };
90bab2243cSGuenter Roeck 
91e1c9d6d6SPaul Cercueil static umode_t hwmon_dev_attr_is_visible(struct kobject *kobj,
92bab2243cSGuenter Roeck 					 struct attribute *attr, int n)
93bab2243cSGuenter Roeck {
9477d76768SYang Li 	struct device *dev = kobj_to_dev(kobj);
95e1c9d6d6SPaul Cercueil 	struct hwmon_device *hdev = to_hwmon_device(dev);
96bab2243cSGuenter Roeck 
97e1c9d6d6SPaul Cercueil 	if (attr == &dev_attr_name.attr && hdev->name == NULL)
98e1c9d6d6SPaul Cercueil 		return 0;
99e1c9d6d6SPaul Cercueil 
100e1c9d6d6SPaul Cercueil 	if (attr == &dev_attr_label.attr && hdev->label == NULL)
101bab2243cSGuenter Roeck 		return 0;
102bab2243cSGuenter Roeck 
103bab2243cSGuenter Roeck 	return attr->mode;
104bab2243cSGuenter Roeck }
105bab2243cSGuenter Roeck 
106524703acSArvind Yadav static const struct attribute_group hwmon_dev_attr_group = {
107bab2243cSGuenter Roeck 	.attrs		= hwmon_dev_attrs,
108e1c9d6d6SPaul Cercueil 	.is_visible	= hwmon_dev_attr_is_visible,
109bab2243cSGuenter Roeck };
110bab2243cSGuenter Roeck 
111bab2243cSGuenter Roeck static const struct attribute_group *hwmon_dev_attr_groups[] = {
112bab2243cSGuenter Roeck 	&hwmon_dev_attr_group,
113bab2243cSGuenter Roeck 	NULL
114bab2243cSGuenter Roeck };
115bab2243cSGuenter Roeck 
1163bf8bdcfSGuenter Roeck static void hwmon_free_attrs(struct attribute **attrs)
1173bf8bdcfSGuenter Roeck {
1183bf8bdcfSGuenter Roeck 	int i;
1193bf8bdcfSGuenter Roeck 
1203bf8bdcfSGuenter Roeck 	for (i = 0; attrs[i]; i++) {
1213bf8bdcfSGuenter Roeck 		struct device_attribute *dattr = to_dev_attr(attrs[i]);
1223bf8bdcfSGuenter Roeck 		struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr);
1233bf8bdcfSGuenter Roeck 
1243bf8bdcfSGuenter Roeck 		kfree(hattr);
1253bf8bdcfSGuenter Roeck 	}
1263bf8bdcfSGuenter Roeck 	kfree(attrs);
1273bf8bdcfSGuenter Roeck }
1283bf8bdcfSGuenter Roeck 
129bab2243cSGuenter Roeck static void hwmon_dev_release(struct device *dev)
130bab2243cSGuenter Roeck {
1313bf8bdcfSGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(dev);
1323bf8bdcfSGuenter Roeck 
1333bf8bdcfSGuenter Roeck 	if (hwdev->group.attrs)
1343bf8bdcfSGuenter Roeck 		hwmon_free_attrs(hwdev->group.attrs);
1353bf8bdcfSGuenter Roeck 	kfree(hwdev->groups);
136e1c9d6d6SPaul Cercueil 	kfree(hwdev->label);
1373bf8bdcfSGuenter Roeck 	kfree(hwdev);
138bab2243cSGuenter Roeck }
139bab2243cSGuenter Roeck 
140*52115fc3SThomas Weißschuh static const struct class hwmon_class = {
141bab2243cSGuenter Roeck 	.name = "hwmon",
142bab2243cSGuenter Roeck 	.dev_groups = hwmon_dev_attr_groups,
143bab2243cSGuenter Roeck 	.dev_release = hwmon_dev_release,
144bab2243cSGuenter Roeck };
1451236441fSMark M. Hoffman 
1464ca5f468SJonathan Cameron static DEFINE_IDA(hwmon_ida);
1471236441fSMark M. Hoffman 
148d560168bSGuenter Roeck /* Thermal zone handling */
149d560168bSGuenter Roeck 
15086430c1aSGuenter Roeck /*
15186430c1aSGuenter Roeck  * The complex conditional is necessary to avoid a cyclic dependency
15286430c1aSGuenter Roeck  * between hwmon and thermal_sys modules.
15386430c1aSGuenter Roeck  */
154f3735332SDaniel Lezcano #ifdef CONFIG_THERMAL_OF
155e5181331SDaniel Lezcano static int hwmon_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
156d560168bSGuenter Roeck {
1570ce637a5SDaniel Lezcano 	struct hwmon_thermal_data *tdata = thermal_zone_device_priv(tz);
1583bf8bdcfSGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
159d560168bSGuenter Roeck 	int ret;
160d560168bSGuenter Roeck 	long t;
161d560168bSGuenter Roeck 
1623bf8bdcfSGuenter Roeck 	ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
163d560168bSGuenter Roeck 				     tdata->index, &t);
164d560168bSGuenter Roeck 	if (ret < 0)
165d560168bSGuenter Roeck 		return ret;
166d560168bSGuenter Roeck 
167d560168bSGuenter Roeck 	*temp = t;
168d560168bSGuenter Roeck 
169d560168bSGuenter Roeck 	return 0;
170d560168bSGuenter Roeck }
171d560168bSGuenter Roeck 
172e5181331SDaniel Lezcano static int hwmon_thermal_set_trips(struct thermal_zone_device *tz, int low, int high)
173a5f6c0f8SDmitry Osipenko {
1740ce637a5SDaniel Lezcano 	struct hwmon_thermal_data *tdata = thermal_zone_device_priv(tz);
175a5f6c0f8SDmitry Osipenko 	struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
176a5f6c0f8SDmitry Osipenko 	const struct hwmon_chip_info *chip = hwdev->chip;
177d8cc9415SKrzysztof Kozlowski 	const struct hwmon_channel_info * const *info = chip->info;
178a5f6c0f8SDmitry Osipenko 	unsigned int i;
179a5f6c0f8SDmitry Osipenko 	int err;
180a5f6c0f8SDmitry Osipenko 
181a5f6c0f8SDmitry Osipenko 	if (!chip->ops->write)
182a5f6c0f8SDmitry Osipenko 		return 0;
183a5f6c0f8SDmitry Osipenko 
184a5f6c0f8SDmitry Osipenko 	for (i = 0; info[i] && info[i]->type != hwmon_temp; i++)
185a5f6c0f8SDmitry Osipenko 		continue;
186a5f6c0f8SDmitry Osipenko 
187a5f6c0f8SDmitry Osipenko 	if (!info[i])
188a5f6c0f8SDmitry Osipenko 		return 0;
189a5f6c0f8SDmitry Osipenko 
190a5f6c0f8SDmitry Osipenko 	if (info[i]->config[tdata->index] & HWMON_T_MIN) {
191a5f6c0f8SDmitry Osipenko 		err = chip->ops->write(tdata->dev, hwmon_temp,
192a5f6c0f8SDmitry Osipenko 				       hwmon_temp_min, tdata->index, low);
193a5f6c0f8SDmitry Osipenko 		if (err && err != -EOPNOTSUPP)
194a5f6c0f8SDmitry Osipenko 			return err;
195a5f6c0f8SDmitry Osipenko 	}
196a5f6c0f8SDmitry Osipenko 
197a5f6c0f8SDmitry Osipenko 	if (info[i]->config[tdata->index] & HWMON_T_MAX) {
198a5f6c0f8SDmitry Osipenko 		err = chip->ops->write(tdata->dev, hwmon_temp,
199a5f6c0f8SDmitry Osipenko 				       hwmon_temp_max, tdata->index, high);
200a5f6c0f8SDmitry Osipenko 		if (err && err != -EOPNOTSUPP)
201a5f6c0f8SDmitry Osipenko 			return err;
202a5f6c0f8SDmitry Osipenko 	}
203a5f6c0f8SDmitry Osipenko 
204a5f6c0f8SDmitry Osipenko 	return 0;
205a5f6c0f8SDmitry Osipenko }
206a5f6c0f8SDmitry Osipenko 
207e5181331SDaniel Lezcano static const struct thermal_zone_device_ops hwmon_thermal_ops = {
208d560168bSGuenter Roeck 	.get_temp = hwmon_thermal_get_temp,
209a5f6c0f8SDmitry Osipenko 	.set_trips = hwmon_thermal_set_trips,
210d560168bSGuenter Roeck };
211d560168bSGuenter Roeck 
2121597b374SGuenter Roeck static void hwmon_thermal_remove_sensor(void *data)
2131597b374SGuenter Roeck {
2141597b374SGuenter Roeck 	list_del(data);
2151597b374SGuenter Roeck }
2161597b374SGuenter Roeck 
2173bf8bdcfSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, int index)
218d560168bSGuenter Roeck {
2191597b374SGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(dev);
220d560168bSGuenter Roeck 	struct hwmon_thermal_data *tdata;
22147c332deSLinus Walleij 	struct thermal_zone_device *tzd;
2221597b374SGuenter Roeck 	int err;
223d560168bSGuenter Roeck 
224d560168bSGuenter Roeck 	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
225d560168bSGuenter Roeck 	if (!tdata)
226d560168bSGuenter Roeck 		return -ENOMEM;
227d560168bSGuenter Roeck 
2283bf8bdcfSGuenter Roeck 	tdata->dev = dev;
229d560168bSGuenter Roeck 	tdata->index = index;
230d560168bSGuenter Roeck 
231e5181331SDaniel Lezcano 	tzd = devm_thermal_of_zone_register(dev, index, tdata,
232d560168bSGuenter Roeck 					    &hwmon_thermal_ops);
2331b5f517cSGuenter Roeck 	if (IS_ERR(tzd)) {
2341b5f517cSGuenter Roeck 		if (PTR_ERR(tzd) != -ENODEV)
23547c332deSLinus Walleij 			return PTR_ERR(tzd);
2361b5f517cSGuenter Roeck 		dev_info(dev, "temp%d_input not attached to any thermal zone\n",
2371b5f517cSGuenter Roeck 			 index + 1);
2381b5f517cSGuenter Roeck 		devm_kfree(dev, tdata);
2391b5f517cSGuenter Roeck 		return 0;
2401b5f517cSGuenter Roeck 	}
241d560168bSGuenter Roeck 
2421597b374SGuenter Roeck 	err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node);
2431597b374SGuenter Roeck 	if (err)
2441597b374SGuenter Roeck 		return err;
2451597b374SGuenter Roeck 
2461597b374SGuenter Roeck 	tdata->tzd = tzd;
2471597b374SGuenter Roeck 	list_add(&tdata->node, &hwdev->tzdata);
2481597b374SGuenter Roeck 
249d560168bSGuenter Roeck 	return 0;
250d560168bSGuenter Roeck }
25144e3ad88SAkinobu Mita 
25244e3ad88SAkinobu Mita static int hwmon_thermal_register_sensors(struct device *dev)
25344e3ad88SAkinobu Mita {
25444e3ad88SAkinobu Mita 	struct hwmon_device *hwdev = to_hwmon_device(dev);
25544e3ad88SAkinobu Mita 	const struct hwmon_chip_info *chip = hwdev->chip;
256d8cc9415SKrzysztof Kozlowski 	const struct hwmon_channel_info * const *info = chip->info;
25744e3ad88SAkinobu Mita 	void *drvdata = dev_get_drvdata(dev);
25844e3ad88SAkinobu Mita 	int i;
25944e3ad88SAkinobu Mita 
26044e3ad88SAkinobu Mita 	for (i = 1; info[i]; i++) {
26144e3ad88SAkinobu Mita 		int j;
26244e3ad88SAkinobu Mita 
26344e3ad88SAkinobu Mita 		if (info[i]->type != hwmon_temp)
26444e3ad88SAkinobu Mita 			continue;
26544e3ad88SAkinobu Mita 
26644e3ad88SAkinobu Mita 		for (j = 0; info[i]->config[j]; j++) {
26744e3ad88SAkinobu Mita 			int err;
26844e3ad88SAkinobu Mita 
26944e3ad88SAkinobu Mita 			if (!(info[i]->config[j] & HWMON_T_INPUT) ||
27044e3ad88SAkinobu Mita 			    !chip->ops->is_visible(drvdata, hwmon_temp,
27144e3ad88SAkinobu Mita 						   hwmon_temp_input, j))
27244e3ad88SAkinobu Mita 				continue;
27344e3ad88SAkinobu Mita 
27444e3ad88SAkinobu Mita 			err = hwmon_thermal_add_sensor(dev, j);
27544e3ad88SAkinobu Mita 			if (err)
27644e3ad88SAkinobu Mita 				return err;
27744e3ad88SAkinobu Mita 		}
27844e3ad88SAkinobu Mita 	}
27944e3ad88SAkinobu Mita 
28044e3ad88SAkinobu Mita 	return 0;
28144e3ad88SAkinobu Mita }
28244e3ad88SAkinobu Mita 
2831597b374SGuenter Roeck static void hwmon_thermal_notify(struct device *dev, int index)
2841597b374SGuenter Roeck {
2851597b374SGuenter Roeck 	struct hwmon_device *hwdev = to_hwmon_device(dev);
2861597b374SGuenter Roeck 	struct hwmon_thermal_data *tzdata;
2871597b374SGuenter Roeck 
2881597b374SGuenter Roeck 	list_for_each_entry(tzdata, &hwdev->tzdata, node) {
2891597b374SGuenter Roeck 		if (tzdata->index == index) {
2901597b374SGuenter Roeck 			thermal_zone_device_update(tzdata->tzd,
2911597b374SGuenter Roeck 						   THERMAL_EVENT_UNSPECIFIED);
2921597b374SGuenter Roeck 		}
2931597b374SGuenter Roeck 	}
2941597b374SGuenter Roeck }
2951597b374SGuenter Roeck 
296d560168bSGuenter Roeck #else
29744e3ad88SAkinobu Mita static int hwmon_thermal_register_sensors(struct device *dev)
298d560168bSGuenter Roeck {
299d560168bSGuenter Roeck 	return 0;
300d560168bSGuenter Roeck }
3011597b374SGuenter Roeck 
3021597b374SGuenter Roeck static void hwmon_thermal_notify(struct device *dev, int index) { }
3031597b374SGuenter Roeck 
30486430c1aSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
305d560168bSGuenter Roeck 
30661b8ab2cSNicolin Chen static int hwmon_attr_base(enum hwmon_sensor_types type)
30761b8ab2cSNicolin Chen {
3084413405fSDr. David Alan Gilbert 	if (type == hwmon_in || type == hwmon_intrusion)
30961b8ab2cSNicolin Chen 		return 0;
31061b8ab2cSNicolin Chen 	return 1;
31161b8ab2cSNicolin Chen }
31261b8ab2cSNicolin Chen 
3134ce6e8a8SGuenter Roeck #if IS_REACHABLE(CONFIG_I2C)
3144ce6e8a8SGuenter Roeck 
3154ce6e8a8SGuenter Roeck /*
3164ce6e8a8SGuenter Roeck  * PEC support
3174ce6e8a8SGuenter Roeck  *
3184ce6e8a8SGuenter Roeck  * The 'pec' attribute is attached to I2C client devices. It is only provided
3194ce6e8a8SGuenter Roeck  * if the i2c controller supports PEC.
3204ce6e8a8SGuenter Roeck  *
3214ce6e8a8SGuenter Roeck  * The mutex ensures that PEC configuration between i2c device and the hardware
3224ce6e8a8SGuenter Roeck  * is consistent. Use a single mutex because attribute writes are supposed to be
3234ce6e8a8SGuenter Roeck  * rare, and maintaining a separate mutex for each hardware monitoring device
3244ce6e8a8SGuenter Roeck  * would add substantial complexity to the driver for little if any gain.
3254ce6e8a8SGuenter Roeck  *
3264ce6e8a8SGuenter Roeck  * The hardware monitoring device is identified as child of the i2c client
3274ce6e8a8SGuenter Roeck  * device. This assumes that only a single hardware monitoring device is
3284ce6e8a8SGuenter Roeck  * attached to an i2c client device.
3294ce6e8a8SGuenter Roeck  */
3304ce6e8a8SGuenter Roeck 
3314ce6e8a8SGuenter Roeck static DEFINE_MUTEX(hwmon_pec_mutex);
3324ce6e8a8SGuenter Roeck 
3334ce6e8a8SGuenter Roeck static int hwmon_match_device(struct device *dev, void *data)
3344ce6e8a8SGuenter Roeck {
3354ce6e8a8SGuenter Roeck 	return dev->class == &hwmon_class;
3364ce6e8a8SGuenter Roeck }
3374ce6e8a8SGuenter Roeck 
3384ce6e8a8SGuenter Roeck static ssize_t pec_show(struct device *dev, struct device_attribute *dummy,
3394ce6e8a8SGuenter Roeck 			char *buf)
3404ce6e8a8SGuenter Roeck {
3414ce6e8a8SGuenter Roeck 	struct i2c_client *client = to_i2c_client(dev);
3424ce6e8a8SGuenter Roeck 
3434ce6e8a8SGuenter Roeck 	return sysfs_emit(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
3444ce6e8a8SGuenter Roeck }
3454ce6e8a8SGuenter Roeck 
3464ce6e8a8SGuenter Roeck static ssize_t pec_store(struct device *dev, struct device_attribute *devattr,
3474ce6e8a8SGuenter Roeck 			 const char *buf, size_t count)
3484ce6e8a8SGuenter Roeck {
3494ce6e8a8SGuenter Roeck 	struct i2c_client *client = to_i2c_client(dev);
3504ce6e8a8SGuenter Roeck 	struct hwmon_device *hwdev;
3514ce6e8a8SGuenter Roeck 	struct device *hdev;
3524ce6e8a8SGuenter Roeck 	bool val;
3534ce6e8a8SGuenter Roeck 	int err;
3544ce6e8a8SGuenter Roeck 
3554ce6e8a8SGuenter Roeck 	err = kstrtobool(buf, &val);
3564ce6e8a8SGuenter Roeck 	if (err < 0)
3574ce6e8a8SGuenter Roeck 		return err;
3584ce6e8a8SGuenter Roeck 
3594ce6e8a8SGuenter Roeck 	hdev = device_find_child(dev, NULL, hwmon_match_device);
3604ce6e8a8SGuenter Roeck 	if (!hdev)
3614ce6e8a8SGuenter Roeck 		return -ENODEV;
3624ce6e8a8SGuenter Roeck 
3634ce6e8a8SGuenter Roeck 	mutex_lock(&hwmon_pec_mutex);
3644ce6e8a8SGuenter Roeck 
3654ce6e8a8SGuenter Roeck 	/*
3664ce6e8a8SGuenter Roeck 	 * If there is no write function, we assume that chip specific
3674ce6e8a8SGuenter Roeck 	 * handling is not required.
3684ce6e8a8SGuenter Roeck 	 */
3694ce6e8a8SGuenter Roeck 	hwdev = to_hwmon_device(hdev);
3704ce6e8a8SGuenter Roeck 	if (hwdev->chip->ops->write) {
3714ce6e8a8SGuenter Roeck 		err = hwdev->chip->ops->write(hdev, hwmon_chip, hwmon_chip_pec, 0, val);
3724ce6e8a8SGuenter Roeck 		if (err && err != -EOPNOTSUPP)
3734ce6e8a8SGuenter Roeck 			goto unlock;
3744ce6e8a8SGuenter Roeck 	}
3754ce6e8a8SGuenter Roeck 
3764ce6e8a8SGuenter Roeck 	if (!val)
3774ce6e8a8SGuenter Roeck 		client->flags &= ~I2C_CLIENT_PEC;
3784ce6e8a8SGuenter Roeck 	else
3794ce6e8a8SGuenter Roeck 		client->flags |= I2C_CLIENT_PEC;
3804ce6e8a8SGuenter Roeck 
3814ce6e8a8SGuenter Roeck 	err = count;
3824ce6e8a8SGuenter Roeck unlock:
3834ce6e8a8SGuenter Roeck 	mutex_unlock(&hwmon_pec_mutex);
3844ce6e8a8SGuenter Roeck 	put_device(hdev);
3854ce6e8a8SGuenter Roeck 
3864ce6e8a8SGuenter Roeck 	return err;
3874ce6e8a8SGuenter Roeck }
3884ce6e8a8SGuenter Roeck 
3894ce6e8a8SGuenter Roeck static DEVICE_ATTR_RW(pec);
3904ce6e8a8SGuenter Roeck 
3914ce6e8a8SGuenter Roeck static void hwmon_remove_pec(void *dev)
3924ce6e8a8SGuenter Roeck {
3934ce6e8a8SGuenter Roeck 	device_remove_file(dev, &dev_attr_pec);
3944ce6e8a8SGuenter Roeck }
3954ce6e8a8SGuenter Roeck 
3964ce6e8a8SGuenter Roeck static int hwmon_pec_register(struct device *hdev)
3974ce6e8a8SGuenter Roeck {
3984ce6e8a8SGuenter Roeck 	struct i2c_client *client = i2c_verify_client(hdev->parent);
3994ce6e8a8SGuenter Roeck 	int err;
4004ce6e8a8SGuenter Roeck 
4014ce6e8a8SGuenter Roeck 	if (!client)
4024ce6e8a8SGuenter Roeck 		return -EINVAL;
4034ce6e8a8SGuenter Roeck 
4044ce6e8a8SGuenter Roeck 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_PEC))
4054ce6e8a8SGuenter Roeck 		return 0;
4064ce6e8a8SGuenter Roeck 
4074ce6e8a8SGuenter Roeck 	err = device_create_file(&client->dev, &dev_attr_pec);
4084ce6e8a8SGuenter Roeck 	if (err)
4094ce6e8a8SGuenter Roeck 		return err;
4104ce6e8a8SGuenter Roeck 
4114ce6e8a8SGuenter Roeck 	return devm_add_action_or_reset(hdev, hwmon_remove_pec, &client->dev);
4124ce6e8a8SGuenter Roeck }
4134ce6e8a8SGuenter Roeck 
4144ce6e8a8SGuenter Roeck #else /* CONFIG_I2C */
4154ce6e8a8SGuenter Roeck static int hwmon_pec_register(struct device *hdev)
4164ce6e8a8SGuenter Roeck {
4174ce6e8a8SGuenter Roeck 	return -EINVAL;
4184ce6e8a8SGuenter Roeck }
4194ce6e8a8SGuenter Roeck #endif /* CONFIG_I2C */
4204ce6e8a8SGuenter Roeck 
421d560168bSGuenter Roeck /* sysfs attribute management */
422d560168bSGuenter Roeck 
423d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev,
424d560168bSGuenter Roeck 			       struct device_attribute *devattr, char *buf)
425d560168bSGuenter Roeck {
426d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
427d560168bSGuenter Roeck 	long val;
428d560168bSGuenter Roeck 	int ret;
429d560168bSGuenter Roeck 
430d560168bSGuenter Roeck 	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
431d560168bSGuenter Roeck 			       &val);
432d560168bSGuenter Roeck 	if (ret < 0)
433d560168bSGuenter Roeck 		return ret;
434d560168bSGuenter Roeck 
43561b8ab2cSNicolin Chen 	trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
43661b8ab2cSNicolin Chen 			      hattr->name, val);
43761b8ab2cSNicolin Chen 
438d560168bSGuenter Roeck 	return sprintf(buf, "%ld\n", val);
439d560168bSGuenter Roeck }
440d560168bSGuenter Roeck 
441e159ab5cSGuenter Roeck static ssize_t hwmon_attr_show_string(struct device *dev,
442e159ab5cSGuenter Roeck 				      struct device_attribute *devattr,
443e159ab5cSGuenter Roeck 				      char *buf)
444e159ab5cSGuenter Roeck {
445e159ab5cSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
44661b8ab2cSNicolin Chen 	enum hwmon_sensor_types type = hattr->type;
4475ba6bcbcSJean Delvare 	const char *s;
448e159ab5cSGuenter Roeck 	int ret;
449e159ab5cSGuenter Roeck 
450e159ab5cSGuenter Roeck 	ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
451e159ab5cSGuenter Roeck 				      hattr->index, &s);
452e159ab5cSGuenter Roeck 	if (ret < 0)
453e159ab5cSGuenter Roeck 		return ret;
454e159ab5cSGuenter Roeck 
45561b8ab2cSNicolin Chen 	trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
45661b8ab2cSNicolin Chen 				     hattr->name, s);
45761b8ab2cSNicolin Chen 
458e159ab5cSGuenter Roeck 	return sprintf(buf, "%s\n", s);
459e159ab5cSGuenter Roeck }
460e159ab5cSGuenter Roeck 
461d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev,
462d560168bSGuenter Roeck 				struct device_attribute *devattr,
463d560168bSGuenter Roeck 				const char *buf, size_t count)
464d560168bSGuenter Roeck {
465d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
466d560168bSGuenter Roeck 	long val;
467d560168bSGuenter Roeck 	int ret;
468d560168bSGuenter Roeck 
469d560168bSGuenter Roeck 	ret = kstrtol(buf, 10, &val);
470d560168bSGuenter Roeck 	if (ret < 0)
471d560168bSGuenter Roeck 		return ret;
472d560168bSGuenter Roeck 
473d560168bSGuenter Roeck 	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
474d560168bSGuenter Roeck 				val);
475d560168bSGuenter Roeck 	if (ret < 0)
476d560168bSGuenter Roeck 		return ret;
477d560168bSGuenter Roeck 
47861b8ab2cSNicolin Chen 	trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
47961b8ab2cSNicolin Chen 			       hattr->name, val);
480d560168bSGuenter Roeck 
48161b8ab2cSNicolin Chen 	return count;
482d560168bSGuenter Roeck }
483d560168bSGuenter Roeck 
484e159ab5cSGuenter Roeck static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
485e159ab5cSGuenter Roeck {
486e159ab5cSGuenter Roeck 	return (type == hwmon_temp && attr == hwmon_temp_label) ||
487e159ab5cSGuenter Roeck 	       (type == hwmon_in && attr == hwmon_in_label) ||
488e159ab5cSGuenter Roeck 	       (type == hwmon_curr && attr == hwmon_curr_label) ||
489e159ab5cSGuenter Roeck 	       (type == hwmon_power && attr == hwmon_power_label) ||
490e159ab5cSGuenter Roeck 	       (type == hwmon_energy && attr == hwmon_energy_label) ||
491e159ab5cSGuenter Roeck 	       (type == hwmon_humidity && attr == hwmon_humidity_label) ||
492e159ab5cSGuenter Roeck 	       (type == hwmon_fan && attr == hwmon_fan_label);
493e159ab5cSGuenter Roeck }
494e159ab5cSGuenter Roeck 
4953bf8bdcfSGuenter Roeck static struct attribute *hwmon_genattr(const void *drvdata,
496d560168bSGuenter Roeck 				       enum hwmon_sensor_types type,
497d560168bSGuenter Roeck 				       u32 attr,
498d560168bSGuenter Roeck 				       int index,
499d560168bSGuenter Roeck 				       const char *template,
500d560168bSGuenter Roeck 				       const struct hwmon_ops *ops)
501d560168bSGuenter Roeck {
502d560168bSGuenter Roeck 	struct hwmon_device_attribute *hattr;
503d560168bSGuenter Roeck 	struct device_attribute *dattr;
504d560168bSGuenter Roeck 	struct attribute *a;
505d560168bSGuenter Roeck 	umode_t mode;
5063b443defSRasmus Villemoes 	const char *name;
507e159ab5cSGuenter Roeck 	bool is_string = is_string_attr(type, attr);
508d560168bSGuenter Roeck 
509d560168bSGuenter Roeck 	mode = ops->is_visible(drvdata, type, attr, index);
510d560168bSGuenter Roeck 	if (!mode)
511d560168bSGuenter Roeck 		return ERR_PTR(-ENOENT);
512d560168bSGuenter Roeck 
5130d87116fSGuenter Roeck 	if ((mode & 0444) && ((is_string && !ops->read_string) ||
514e159ab5cSGuenter Roeck 				 (!is_string && !ops->read)))
515d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
5160d87116fSGuenter Roeck 	if ((mode & 0222) && !ops->write)
517d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
518d560168bSGuenter Roeck 
5193bf8bdcfSGuenter Roeck 	hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
520d560168bSGuenter Roeck 	if (!hattr)
521d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
522d560168bSGuenter Roeck 
5233a412d5eSGuenter Roeck 	if (type == hwmon_chip) {
5243b443defSRasmus Villemoes 		name = template;
5253a412d5eSGuenter Roeck 	} else {
5263a412d5eSGuenter Roeck 		scnprintf(hattr->name, sizeof(hattr->name), template,
5273a412d5eSGuenter Roeck 			  index + hwmon_attr_base(type));
5283a412d5eSGuenter Roeck 		name = hattr->name;
5293a412d5eSGuenter Roeck 	}
5303a412d5eSGuenter Roeck 
531d560168bSGuenter Roeck 	hattr->type = type;
532d560168bSGuenter Roeck 	hattr->attr = attr;
533d560168bSGuenter Roeck 	hattr->index = index;
534d560168bSGuenter Roeck 	hattr->ops = ops;
535d560168bSGuenter Roeck 
536d560168bSGuenter Roeck 	dattr = &hattr->dev_attr;
537e159ab5cSGuenter Roeck 	dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
538d560168bSGuenter Roeck 	dattr->store = hwmon_attr_store;
539d560168bSGuenter Roeck 
540d560168bSGuenter Roeck 	a = &dattr->attr;
541d560168bSGuenter Roeck 	sysfs_attr_init(a);
542d560168bSGuenter Roeck 	a->name = name;
543d560168bSGuenter Roeck 	a->mode = mode;
544d560168bSGuenter Roeck 
545d560168bSGuenter Roeck 	return a;
546d560168bSGuenter Roeck }
547d560168bSGuenter Roeck 
548f4d325d5SGuenter Roeck /*
549f4d325d5SGuenter Roeck  * Chip attributes are not attribute templates but actual sysfs attributes.
550f4d325d5SGuenter Roeck  * See hwmon_genattr() for special handling.
551f4d325d5SGuenter Roeck  */
552f4d325d5SGuenter Roeck static const char * const hwmon_chip_attrs[] = {
553d560168bSGuenter Roeck 	[hwmon_chip_temp_reset_history] = "temp_reset_history",
55400d616cfSGuenter Roeck 	[hwmon_chip_in_reset_history] = "in_reset_history",
5559b26947cSGuenter Roeck 	[hwmon_chip_curr_reset_history] = "curr_reset_history",
556b308f5c7SGuenter Roeck 	[hwmon_chip_power_reset_history] = "power_reset_history",
557d560168bSGuenter Roeck 	[hwmon_chip_update_interval] = "update_interval",
558d560168bSGuenter Roeck 	[hwmon_chip_alarms] = "alarms",
5599f00995eSGuenter Roeck 	[hwmon_chip_samples] = "samples",
5609f00995eSGuenter Roeck 	[hwmon_chip_curr_samples] = "curr_samples",
5619f00995eSGuenter Roeck 	[hwmon_chip_in_samples] = "in_samples",
5629f00995eSGuenter Roeck 	[hwmon_chip_power_samples] = "power_samples",
5639f00995eSGuenter Roeck 	[hwmon_chip_temp_samples] = "temp_samples",
564fe6ac237SJames Seo 	[hwmon_chip_beep_enable] = "beep_enable",
565d560168bSGuenter Roeck };
566d560168bSGuenter Roeck 
567d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = {
568002c6b54SGuenter Roeck 	[hwmon_temp_enable] = "temp%d_enable",
569d560168bSGuenter Roeck 	[hwmon_temp_input] = "temp%d_input",
570d560168bSGuenter Roeck 	[hwmon_temp_type] = "temp%d_type",
571d560168bSGuenter Roeck 	[hwmon_temp_lcrit] = "temp%d_lcrit",
572d560168bSGuenter Roeck 	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
573d560168bSGuenter Roeck 	[hwmon_temp_min] = "temp%d_min",
574d560168bSGuenter Roeck 	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
575d560168bSGuenter Roeck 	[hwmon_temp_max] = "temp%d_max",
576d560168bSGuenter Roeck 	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
577d560168bSGuenter Roeck 	[hwmon_temp_crit] = "temp%d_crit",
578d560168bSGuenter Roeck 	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
579d560168bSGuenter Roeck 	[hwmon_temp_emergency] = "temp%d_emergency",
580d560168bSGuenter Roeck 	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
581d560168bSGuenter Roeck 	[hwmon_temp_alarm] = "temp%d_alarm",
582d560168bSGuenter Roeck 	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
583d560168bSGuenter Roeck 	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
584d560168bSGuenter Roeck 	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
585d560168bSGuenter Roeck 	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
586d560168bSGuenter Roeck 	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
587d560168bSGuenter Roeck 	[hwmon_temp_fault] = "temp%d_fault",
588d560168bSGuenter Roeck 	[hwmon_temp_offset] = "temp%d_offset",
589d560168bSGuenter Roeck 	[hwmon_temp_label] = "temp%d_label",
590d560168bSGuenter Roeck 	[hwmon_temp_lowest] = "temp%d_lowest",
591d560168bSGuenter Roeck 	[hwmon_temp_highest] = "temp%d_highest",
592d560168bSGuenter Roeck 	[hwmon_temp_reset_history] = "temp%d_reset_history",
5931967f712SZbigniew Lukwinski 	[hwmon_temp_rated_min] = "temp%d_rated_min",
5941967f712SZbigniew Lukwinski 	[hwmon_temp_rated_max] = "temp%d_rated_max",
595fe6ac237SJames Seo 	[hwmon_temp_beep] = "temp%d_beep",
596d560168bSGuenter Roeck };
597d560168bSGuenter Roeck 
59800d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = {
599002c6b54SGuenter Roeck 	[hwmon_in_enable] = "in%d_enable",
60000d616cfSGuenter Roeck 	[hwmon_in_input] = "in%d_input",
60100d616cfSGuenter Roeck 	[hwmon_in_min] = "in%d_min",
60200d616cfSGuenter Roeck 	[hwmon_in_max] = "in%d_max",
60300d616cfSGuenter Roeck 	[hwmon_in_lcrit] = "in%d_lcrit",
60400d616cfSGuenter Roeck 	[hwmon_in_crit] = "in%d_crit",
60500d616cfSGuenter Roeck 	[hwmon_in_average] = "in%d_average",
60600d616cfSGuenter Roeck 	[hwmon_in_lowest] = "in%d_lowest",
60700d616cfSGuenter Roeck 	[hwmon_in_highest] = "in%d_highest",
60800d616cfSGuenter Roeck 	[hwmon_in_reset_history] = "in%d_reset_history",
60900d616cfSGuenter Roeck 	[hwmon_in_label] = "in%d_label",
61000d616cfSGuenter Roeck 	[hwmon_in_alarm] = "in%d_alarm",
61100d616cfSGuenter Roeck 	[hwmon_in_min_alarm] = "in%d_min_alarm",
61200d616cfSGuenter Roeck 	[hwmon_in_max_alarm] = "in%d_max_alarm",
61300d616cfSGuenter Roeck 	[hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
61400d616cfSGuenter Roeck 	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
6151967f712SZbigniew Lukwinski 	[hwmon_in_rated_min] = "in%d_rated_min",
6161967f712SZbigniew Lukwinski 	[hwmon_in_rated_max] = "in%d_rated_max",
617fe6ac237SJames Seo 	[hwmon_in_beep] = "in%d_beep",
61835c1bfb9SNuno Sa 	[hwmon_in_fault] = "in%d_fault",
61900d616cfSGuenter Roeck };
62000d616cfSGuenter Roeck 
6219b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = {
622002c6b54SGuenter Roeck 	[hwmon_curr_enable] = "curr%d_enable",
6239b26947cSGuenter Roeck 	[hwmon_curr_input] = "curr%d_input",
6249b26947cSGuenter Roeck 	[hwmon_curr_min] = "curr%d_min",
6259b26947cSGuenter Roeck 	[hwmon_curr_max] = "curr%d_max",
6269b26947cSGuenter Roeck 	[hwmon_curr_lcrit] = "curr%d_lcrit",
6279b26947cSGuenter Roeck 	[hwmon_curr_crit] = "curr%d_crit",
6289b26947cSGuenter Roeck 	[hwmon_curr_average] = "curr%d_average",
6299b26947cSGuenter Roeck 	[hwmon_curr_lowest] = "curr%d_lowest",
6309b26947cSGuenter Roeck 	[hwmon_curr_highest] = "curr%d_highest",
6319b26947cSGuenter Roeck 	[hwmon_curr_reset_history] = "curr%d_reset_history",
6329b26947cSGuenter Roeck 	[hwmon_curr_label] = "curr%d_label",
6339b26947cSGuenter Roeck 	[hwmon_curr_alarm] = "curr%d_alarm",
6349b26947cSGuenter Roeck 	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
6359b26947cSGuenter Roeck 	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
6369b26947cSGuenter Roeck 	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
6379b26947cSGuenter Roeck 	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
6381967f712SZbigniew Lukwinski 	[hwmon_curr_rated_min] = "curr%d_rated_min",
6391967f712SZbigniew Lukwinski 	[hwmon_curr_rated_max] = "curr%d_rated_max",
640fe6ac237SJames Seo 	[hwmon_curr_beep] = "curr%d_beep",
6419b26947cSGuenter Roeck };
6429b26947cSGuenter Roeck 
643b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = {
644002c6b54SGuenter Roeck 	[hwmon_power_enable] = "power%d_enable",
645b308f5c7SGuenter Roeck 	[hwmon_power_average] = "power%d_average",
646b308f5c7SGuenter Roeck 	[hwmon_power_average_interval] = "power%d_average_interval",
647b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_max] = "power%d_interval_max",
648b308f5c7SGuenter Roeck 	[hwmon_power_average_interval_min] = "power%d_interval_min",
649b308f5c7SGuenter Roeck 	[hwmon_power_average_highest] = "power%d_average_highest",
650b308f5c7SGuenter Roeck 	[hwmon_power_average_lowest] = "power%d_average_lowest",
651b308f5c7SGuenter Roeck 	[hwmon_power_average_max] = "power%d_average_max",
652b308f5c7SGuenter Roeck 	[hwmon_power_average_min] = "power%d_average_min",
653b308f5c7SGuenter Roeck 	[hwmon_power_input] = "power%d_input",
654b308f5c7SGuenter Roeck 	[hwmon_power_input_highest] = "power%d_input_highest",
655b308f5c7SGuenter Roeck 	[hwmon_power_input_lowest] = "power%d_input_lowest",
656b308f5c7SGuenter Roeck 	[hwmon_power_reset_history] = "power%d_reset_history",
657b308f5c7SGuenter Roeck 	[hwmon_power_accuracy] = "power%d_accuracy",
658b308f5c7SGuenter Roeck 	[hwmon_power_cap] = "power%d_cap",
659b308f5c7SGuenter Roeck 	[hwmon_power_cap_hyst] = "power%d_cap_hyst",
660b308f5c7SGuenter Roeck 	[hwmon_power_cap_max] = "power%d_cap_max",
661b308f5c7SGuenter Roeck 	[hwmon_power_cap_min] = "power%d_cap_min",
662aa7f29b0SAndrew Lunn 	[hwmon_power_min] = "power%d_min",
663b308f5c7SGuenter Roeck 	[hwmon_power_max] = "power%d_max",
664aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit] = "power%d_lcrit",
665b308f5c7SGuenter Roeck 	[hwmon_power_crit] = "power%d_crit",
666b308f5c7SGuenter Roeck 	[hwmon_power_label] = "power%d_label",
667b308f5c7SGuenter Roeck 	[hwmon_power_alarm] = "power%d_alarm",
668b308f5c7SGuenter Roeck 	[hwmon_power_cap_alarm] = "power%d_cap_alarm",
669aa7f29b0SAndrew Lunn 	[hwmon_power_min_alarm] = "power%d_min_alarm",
670b308f5c7SGuenter Roeck 	[hwmon_power_max_alarm] = "power%d_max_alarm",
671aa7f29b0SAndrew Lunn 	[hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
672b308f5c7SGuenter Roeck 	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
6731967f712SZbigniew Lukwinski 	[hwmon_power_rated_min] = "power%d_rated_min",
6741967f712SZbigniew Lukwinski 	[hwmon_power_rated_max] = "power%d_rated_max",
675b308f5c7SGuenter Roeck };
676b308f5c7SGuenter Roeck 
6776bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = {
678002c6b54SGuenter Roeck 	[hwmon_energy_enable] = "energy%d_enable",
6796bfcca44SGuenter Roeck 	[hwmon_energy_input] = "energy%d_input",
6806bfcca44SGuenter Roeck 	[hwmon_energy_label] = "energy%d_label",
6816bfcca44SGuenter Roeck };
6826bfcca44SGuenter Roeck 
6836bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = {
684002c6b54SGuenter Roeck 	[hwmon_humidity_enable] = "humidity%d_enable",
6856bfcca44SGuenter Roeck 	[hwmon_humidity_input] = "humidity%d_input",
6866bfcca44SGuenter Roeck 	[hwmon_humidity_label] = "humidity%d_label",
6876bfcca44SGuenter Roeck 	[hwmon_humidity_min] = "humidity%d_min",
6886bfcca44SGuenter Roeck 	[hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
6896bfcca44SGuenter Roeck 	[hwmon_humidity_max] = "humidity%d_max",
6906bfcca44SGuenter Roeck 	[hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
6916bfcca44SGuenter Roeck 	[hwmon_humidity_alarm] = "humidity%d_alarm",
6926bfcca44SGuenter Roeck 	[hwmon_humidity_fault] = "humidity%d_fault",
6931967f712SZbigniew Lukwinski 	[hwmon_humidity_rated_min] = "humidity%d_rated_min",
6941967f712SZbigniew Lukwinski 	[hwmon_humidity_rated_max] = "humidity%d_rated_max",
6955f85c4d1SJavier Carrasco 	[hwmon_humidity_min_alarm] = "humidity%d_min_alarm",
6965f85c4d1SJavier Carrasco 	[hwmon_humidity_max_alarm] = "humidity%d_max_alarm",
6976bfcca44SGuenter Roeck };
6986bfcca44SGuenter Roeck 
6998faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = {
700002c6b54SGuenter Roeck 	[hwmon_fan_enable] = "fan%d_enable",
7018faee73fSGuenter Roeck 	[hwmon_fan_input] = "fan%d_input",
7028faee73fSGuenter Roeck 	[hwmon_fan_label] = "fan%d_label",
7038faee73fSGuenter Roeck 	[hwmon_fan_min] = "fan%d_min",
7048faee73fSGuenter Roeck 	[hwmon_fan_max] = "fan%d_max",
7058faee73fSGuenter Roeck 	[hwmon_fan_div] = "fan%d_div",
7068faee73fSGuenter Roeck 	[hwmon_fan_pulses] = "fan%d_pulses",
7078faee73fSGuenter Roeck 	[hwmon_fan_target] = "fan%d_target",
7088faee73fSGuenter Roeck 	[hwmon_fan_alarm] = "fan%d_alarm",
7098faee73fSGuenter Roeck 	[hwmon_fan_min_alarm] = "fan%d_min_alarm",
7108faee73fSGuenter Roeck 	[hwmon_fan_max_alarm] = "fan%d_max_alarm",
7118faee73fSGuenter Roeck 	[hwmon_fan_fault] = "fan%d_fault",
712fe6ac237SJames Seo 	[hwmon_fan_beep] = "fan%d_beep",
7138faee73fSGuenter Roeck };
7148faee73fSGuenter Roeck 
715f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = {
716f9f7bb3aSGuenter Roeck 	[hwmon_pwm_input] = "pwm%d",
717f9f7bb3aSGuenter Roeck 	[hwmon_pwm_enable] = "pwm%d_enable",
718f9f7bb3aSGuenter Roeck 	[hwmon_pwm_mode] = "pwm%d_mode",
719f9f7bb3aSGuenter Roeck 	[hwmon_pwm_freq] = "pwm%d_freq",
720e75d16e5SArmin Wolf 	[hwmon_pwm_auto_channels_temp] = "pwm%d_auto_channels_temp",
721f9f7bb3aSGuenter Roeck };
722f9f7bb3aSGuenter Roeck 
7234413405fSDr. David Alan Gilbert static const char * const hwmon_intrusion_attr_templates[] = {
7244413405fSDr. David Alan Gilbert 	[hwmon_intrusion_alarm] = "intrusion%d_alarm",
7254413405fSDr. David Alan Gilbert 	[hwmon_intrusion_beep]  = "intrusion%d_beep",
7264413405fSDr. David Alan Gilbert };
7274413405fSDr. David Alan Gilbert 
728d560168bSGuenter Roeck static const char * const *__templates[] = {
729f4d325d5SGuenter Roeck 	[hwmon_chip] = hwmon_chip_attrs,
730d560168bSGuenter Roeck 	[hwmon_temp] = hwmon_temp_attr_templates,
73100d616cfSGuenter Roeck 	[hwmon_in] = hwmon_in_attr_templates,
7329b26947cSGuenter Roeck 	[hwmon_curr] = hwmon_curr_attr_templates,
733b308f5c7SGuenter Roeck 	[hwmon_power] = hwmon_power_attr_templates,
7346bfcca44SGuenter Roeck 	[hwmon_energy] = hwmon_energy_attr_templates,
7356bfcca44SGuenter Roeck 	[hwmon_humidity] = hwmon_humidity_attr_templates,
7368faee73fSGuenter Roeck 	[hwmon_fan] = hwmon_fan_attr_templates,
737f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = hwmon_pwm_attr_templates,
7384413405fSDr. David Alan Gilbert 	[hwmon_intrusion] = hwmon_intrusion_attr_templates,
739d560168bSGuenter Roeck };
740d560168bSGuenter Roeck 
741d560168bSGuenter Roeck static const int __templates_size[] = {
742f4d325d5SGuenter Roeck 	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
743d560168bSGuenter Roeck 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
74400d616cfSGuenter Roeck 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
7459b26947cSGuenter Roeck 	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
746b308f5c7SGuenter Roeck 	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
7476bfcca44SGuenter Roeck 	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
7486bfcca44SGuenter Roeck 	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
7498faee73fSGuenter Roeck 	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
750f9f7bb3aSGuenter Roeck 	[hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
7514413405fSDr. David Alan Gilbert 	[hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
752d560168bSGuenter Roeck };
753d560168bSGuenter Roeck 
7541597b374SGuenter Roeck int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
7551597b374SGuenter Roeck 		       u32 attr, int channel)
7561597b374SGuenter Roeck {
7577f3cc8f8SGuenter Roeck 	char event[MAX_SYSFS_ATTR_NAME_LENGTH + 5];
7581597b374SGuenter Roeck 	char sattr[MAX_SYSFS_ATTR_NAME_LENGTH];
7597f3cc8f8SGuenter Roeck 	char *envp[] = { event, NULL };
7601597b374SGuenter Roeck 	const char * const *templates;
7611597b374SGuenter Roeck 	const char *template;
7621597b374SGuenter Roeck 	int base;
7631597b374SGuenter Roeck 
7641597b374SGuenter Roeck 	if (type >= ARRAY_SIZE(__templates))
7651597b374SGuenter Roeck 		return -EINVAL;
7661597b374SGuenter Roeck 	if (attr >= __templates_size[type])
7671597b374SGuenter Roeck 		return -EINVAL;
7681597b374SGuenter Roeck 
7691597b374SGuenter Roeck 	templates = __templates[type];
7701597b374SGuenter Roeck 	template = templates[attr];
7711597b374SGuenter Roeck 
7721597b374SGuenter Roeck 	base = hwmon_attr_base(type);
7731597b374SGuenter Roeck 
7741597b374SGuenter Roeck 	scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel);
7757f3cc8f8SGuenter Roeck 	scnprintf(event, sizeof(event), "NAME=%s", sattr);
7761597b374SGuenter Roeck 	sysfs_notify(&dev->kobj, NULL, sattr);
7777f3cc8f8SGuenter Roeck 	kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
7781597b374SGuenter Roeck 
7791597b374SGuenter Roeck 	if (type == hwmon_temp)
7801597b374SGuenter Roeck 		hwmon_thermal_notify(dev, channel);
7811597b374SGuenter Roeck 
7821597b374SGuenter Roeck 	return 0;
7831597b374SGuenter Roeck }
7841597b374SGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_notify_event);
7851597b374SGuenter Roeck 
786d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
787d560168bSGuenter Roeck {
788d560168bSGuenter Roeck 	int i, n;
789d560168bSGuenter Roeck 
790d560168bSGuenter Roeck 	for (i = n = 0; info->config[i]; i++)
791d560168bSGuenter Roeck 		n += hweight32(info->config[i]);
792d560168bSGuenter Roeck 
793d560168bSGuenter Roeck 	return n;
794d560168bSGuenter Roeck }
795d560168bSGuenter Roeck 
7963bf8bdcfSGuenter Roeck static int hwmon_genattrs(const void *drvdata,
797d560168bSGuenter Roeck 			  struct attribute **attrs,
798d560168bSGuenter Roeck 			  const struct hwmon_ops *ops,
799d560168bSGuenter Roeck 			  const struct hwmon_channel_info *info)
800d560168bSGuenter Roeck {
801d560168bSGuenter Roeck 	const char * const *templates;
802d560168bSGuenter Roeck 	int template_size;
803d560168bSGuenter Roeck 	int i, aindex = 0;
804d560168bSGuenter Roeck 
805d560168bSGuenter Roeck 	if (info->type >= ARRAY_SIZE(__templates))
806d560168bSGuenter Roeck 		return -EINVAL;
807d560168bSGuenter Roeck 
808d560168bSGuenter Roeck 	templates = __templates[info->type];
809d560168bSGuenter Roeck 	template_size = __templates_size[info->type];
810d560168bSGuenter Roeck 
811d560168bSGuenter Roeck 	for (i = 0; info->config[i]; i++) {
812d560168bSGuenter Roeck 		u32 attr_mask = info->config[i];
813d560168bSGuenter Roeck 		u32 attr;
814d560168bSGuenter Roeck 
815d560168bSGuenter Roeck 		while (attr_mask) {
816d560168bSGuenter Roeck 			struct attribute *a;
817d560168bSGuenter Roeck 
818d560168bSGuenter Roeck 			attr = __ffs(attr_mask);
819d560168bSGuenter Roeck 			attr_mask &= ~BIT(attr);
8204ce6e8a8SGuenter Roeck 			if (attr >= template_size || !templates[attr])
8214ce6e8a8SGuenter Roeck 				continue;	/* attribute is invisible */
8223bf8bdcfSGuenter Roeck 			a = hwmon_genattr(drvdata, info->type, attr, i,
823d560168bSGuenter Roeck 					  templates[attr], ops);
824d560168bSGuenter Roeck 			if (IS_ERR(a)) {
825d560168bSGuenter Roeck 				if (PTR_ERR(a) != -ENOENT)
826d560168bSGuenter Roeck 					return PTR_ERR(a);
827d560168bSGuenter Roeck 				continue;
828d560168bSGuenter Roeck 			}
829d560168bSGuenter Roeck 			attrs[aindex++] = a;
830d560168bSGuenter Roeck 		}
831d560168bSGuenter Roeck 	}
832d560168bSGuenter Roeck 	return aindex;
833d560168bSGuenter Roeck }
834d560168bSGuenter Roeck 
835d560168bSGuenter Roeck static struct attribute **
8363bf8bdcfSGuenter Roeck __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
837d560168bSGuenter Roeck {
838d560168bSGuenter Roeck 	int ret, i, aindex = 0, nattrs = 0;
839d560168bSGuenter Roeck 	struct attribute **attrs;
840d560168bSGuenter Roeck 
841d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++)
842d560168bSGuenter Roeck 		nattrs += hwmon_num_channel_attrs(chip->info[i]);
843d560168bSGuenter Roeck 
844d560168bSGuenter Roeck 	if (nattrs == 0)
845d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
846d560168bSGuenter Roeck 
8473bf8bdcfSGuenter Roeck 	attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
848d560168bSGuenter Roeck 	if (!attrs)
849d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
850d560168bSGuenter Roeck 
851d560168bSGuenter Roeck 	for (i = 0; chip->info[i]; i++) {
8523bf8bdcfSGuenter Roeck 		ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
853d560168bSGuenter Roeck 				     chip->info[i]);
8543bf8bdcfSGuenter Roeck 		if (ret < 0) {
8553bf8bdcfSGuenter Roeck 			hwmon_free_attrs(attrs);
856d560168bSGuenter Roeck 			return ERR_PTR(ret);
8573bf8bdcfSGuenter Roeck 		}
858d560168bSGuenter Roeck 		aindex += ret;
859d560168bSGuenter Roeck 	}
860d560168bSGuenter Roeck 
861d560168bSGuenter Roeck 	return attrs;
862d560168bSGuenter Roeck }
863d560168bSGuenter Roeck 
864d560168bSGuenter Roeck static struct device *
865d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
866d560168bSGuenter Roeck 			const struct hwmon_chip_info *chip,
867d560168bSGuenter Roeck 			const struct attribute_group **groups)
868d560168bSGuenter Roeck {
869d560168bSGuenter Roeck 	struct hwmon_device *hwdev;
870e1c9d6d6SPaul Cercueil 	const char *label;
871d560168bSGuenter Roeck 	struct device *hdev;
8722315332eSPhinex Hung 	struct device *tdev = dev;
87344e3ad88SAkinobu Mita 	int i, err, id;
874d560168bSGuenter Roeck 
87574d3b641SGuenter Roeck 	/* Complain about invalid characters in hwmon name attribute */
876d560168bSGuenter Roeck 	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
87774d3b641SGuenter Roeck 		dev_warn(dev,
87874d3b641SGuenter Roeck 			 "hwmon: '%s' is not a valid name attribute, please fix\n",
87974d3b641SGuenter Roeck 			 name);
880d560168bSGuenter Roeck 
881718fbfa5Skeliu 	id = ida_alloc(&hwmon_ida, GFP_KERNEL);
882d560168bSGuenter Roeck 	if (id < 0)
883d560168bSGuenter Roeck 		return ERR_PTR(id);
884d560168bSGuenter Roeck 
885d560168bSGuenter Roeck 	hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
886d560168bSGuenter Roeck 	if (hwdev == NULL) {
887d560168bSGuenter Roeck 		err = -ENOMEM;
888d560168bSGuenter Roeck 		goto ida_remove;
889d560168bSGuenter Roeck 	}
890d560168bSGuenter Roeck 
891d560168bSGuenter Roeck 	hdev = &hwdev->dev;
892d560168bSGuenter Roeck 
893239552f4SGuenter Roeck 	if (chip) {
894d560168bSGuenter Roeck 		struct attribute **attrs;
895b2a4cc3aSGuenter Roeck 		int ngroups = 2; /* terminating NULL plus &hwdev->groups */
896d560168bSGuenter Roeck 
897d560168bSGuenter Roeck 		if (groups)
898d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
899d560168bSGuenter Roeck 				ngroups++;
900d560168bSGuenter Roeck 
9013bf8bdcfSGuenter Roeck 		hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
90238d8ed65SColin Ian King 		if (!hwdev->groups) {
90338d8ed65SColin Ian King 			err = -ENOMEM;
90438d8ed65SColin Ian King 			goto free_hwmon;
90538d8ed65SColin Ian King 		}
906d560168bSGuenter Roeck 
9073bf8bdcfSGuenter Roeck 		attrs = __hwmon_create_attrs(drvdata, chip);
908d560168bSGuenter Roeck 		if (IS_ERR(attrs)) {
909d560168bSGuenter Roeck 			err = PTR_ERR(attrs);
910d560168bSGuenter Roeck 			goto free_hwmon;
911d560168bSGuenter Roeck 		}
912d560168bSGuenter Roeck 
913d560168bSGuenter Roeck 		hwdev->group.attrs = attrs;
914d560168bSGuenter Roeck 		ngroups = 0;
915d560168bSGuenter Roeck 		hwdev->groups[ngroups++] = &hwdev->group;
916d560168bSGuenter Roeck 
917d560168bSGuenter Roeck 		if (groups) {
918d560168bSGuenter Roeck 			for (i = 0; groups[i]; i++)
919d560168bSGuenter Roeck 				hwdev->groups[ngroups++] = groups[i];
920d560168bSGuenter Roeck 		}
921d560168bSGuenter Roeck 
922d560168bSGuenter Roeck 		hdev->groups = hwdev->groups;
923d560168bSGuenter Roeck 	} else {
924d560168bSGuenter Roeck 		hdev->groups = groups;
925d560168bSGuenter Roeck 	}
926d560168bSGuenter Roeck 
92707320c91SPaul Cercueil 	if (dev && device_property_present(dev, "label")) {
928e1c9d6d6SPaul Cercueil 		err = device_property_read_string(dev, "label", &label);
929e1c9d6d6SPaul Cercueil 		if (err < 0)
930e1c9d6d6SPaul Cercueil 			goto free_hwmon;
931e1c9d6d6SPaul Cercueil 
932e1c9d6d6SPaul Cercueil 		hwdev->label = kstrdup(label, GFP_KERNEL);
933e1c9d6d6SPaul Cercueil 		if (hwdev->label == NULL) {
934e1c9d6d6SPaul Cercueil 			err = -ENOMEM;
935e1c9d6d6SPaul Cercueil 			goto free_hwmon;
936e1c9d6d6SPaul Cercueil 		}
937e1c9d6d6SPaul Cercueil 	}
938e1c9d6d6SPaul Cercueil 
939d560168bSGuenter Roeck 	hwdev->name = name;
940d560168bSGuenter Roeck 	hdev->class = &hwmon_class;
941d560168bSGuenter Roeck 	hdev->parent = dev;
9422315332eSPhinex Hung 	while (tdev && !tdev->of_node)
9432315332eSPhinex Hung 		tdev = tdev->parent;
9442315332eSPhinex Hung 	hdev->of_node = tdev ? tdev->of_node : NULL;
945d560168bSGuenter Roeck 	hwdev->chip = chip;
946d560168bSGuenter Roeck 	dev_set_drvdata(hdev, drvdata);
947d560168bSGuenter Roeck 	dev_set_name(hdev, HWMON_ID_FORMAT, id);
948d560168bSGuenter Roeck 	err = device_register(hdev);
949ada61aa0SYang Yingliang 	if (err) {
950ada61aa0SYang Yingliang 		put_device(hdev);
951ada61aa0SYang Yingliang 		goto ida_remove;
952ada61aa0SYang Yingliang 	}
953d560168bSGuenter Roeck 
9541597b374SGuenter Roeck 	INIT_LIST_HEAD(&hwdev->tzdata);
9551597b374SGuenter Roeck 
9562315332eSPhinex Hung 	if (hdev->of_node && chip && chip->ops->read &&
9574ce6e8a8SGuenter Roeck 	    chip->info[0]->type == hwmon_chip) {
9584ce6e8a8SGuenter Roeck 		u32 config = chip->info[0]->config[0];
9594ce6e8a8SGuenter Roeck 
9604ce6e8a8SGuenter Roeck 		if (config & HWMON_C_REGISTER_TZ) {
96144e3ad88SAkinobu Mita 			err = hwmon_thermal_register_sensors(hdev);
96274e35127SDmitry Osipenko 			if (err) {
96374e35127SDmitry Osipenko 				device_unregister(hdev);
964792eac18SGuenter Roeck 				/*
9654ce6e8a8SGuenter Roeck 				 * Don't worry about hwdev; hwmon_dev_release(),
9664ce6e8a8SGuenter Roeck 				 * called from device_unregister(), will free it.
967792eac18SGuenter Roeck 				 */
96874e35127SDmitry Osipenko 				goto ida_remove;
96974e35127SDmitry Osipenko 			}
97047c332deSLinus Walleij 		}
9714ce6e8a8SGuenter Roeck 		if (config & HWMON_C_PEC) {
9724ce6e8a8SGuenter Roeck 			err = hwmon_pec_register(hdev);
9734ce6e8a8SGuenter Roeck 			if (err) {
9744ce6e8a8SGuenter Roeck 				device_unregister(hdev);
9754ce6e8a8SGuenter Roeck 				goto ida_remove;
9764ce6e8a8SGuenter Roeck 			}
9774ce6e8a8SGuenter Roeck 		}
9784ce6e8a8SGuenter Roeck 	}
979d560168bSGuenter Roeck 
980d560168bSGuenter Roeck 	return hdev;
981d560168bSGuenter Roeck 
982d560168bSGuenter Roeck free_hwmon:
9833bf8bdcfSGuenter Roeck 	hwmon_dev_release(hdev);
984d560168bSGuenter Roeck ida_remove:
985718fbfa5Skeliu 	ida_free(&hwmon_ida, id);
986d560168bSGuenter Roeck 	return ERR_PTR(err);
987d560168bSGuenter Roeck }
988d560168bSGuenter Roeck 
9891236441fSMark M. Hoffman /**
990bab2243cSGuenter Roeck  * hwmon_device_register_with_groups - register w/ hwmon
991bab2243cSGuenter Roeck  * @dev: the parent device
992bab2243cSGuenter Roeck  * @name: hwmon name attribute
993bab2243cSGuenter Roeck  * @drvdata: driver data to attach to created device
994bab2243cSGuenter Roeck  * @groups: List of attribute groups to create
995bab2243cSGuenter Roeck  *
996bab2243cSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
997bab2243cSGuenter Roeck  * longer needed.
998bab2243cSGuenter Roeck  *
999bab2243cSGuenter Roeck  * Returns the pointer to the new device.
1000bab2243cSGuenter Roeck  */
1001bab2243cSGuenter Roeck struct device *
1002bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name,
1003bab2243cSGuenter Roeck 				  void *drvdata,
1004bab2243cSGuenter Roeck 				  const struct attribute_group **groups)
1005bab2243cSGuenter Roeck {
10068353863aSGuenter Roeck 	if (!name)
10078353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
10088353863aSGuenter Roeck 
1009d560168bSGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
1010bab2243cSGuenter Roeck }
1011bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
1012bab2243cSGuenter Roeck 
1013bab2243cSGuenter Roeck /**
1014d560168bSGuenter Roeck  * hwmon_device_register_with_info - register w/ hwmon
1015ddaefa20SGuenter Roeck  * @dev: the parent device (mandatory)
1016ddaefa20SGuenter Roeck  * @name: hwmon name attribute (mandatory)
1017ddaefa20SGuenter Roeck  * @drvdata: driver data to attach to created device (optional)
1018ddaefa20SGuenter Roeck  * @chip: pointer to hwmon chip information (mandatory)
1019848ba0a2SGuenter Roeck  * @extra_groups: pointer to list of additional non-standard attribute groups
1020ddaefa20SGuenter Roeck  *	(optional)
1021d560168bSGuenter Roeck  *
1022d560168bSGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
1023d560168bSGuenter Roeck  * longer needed.
1024d560168bSGuenter Roeck  *
1025d560168bSGuenter Roeck  * Returns the pointer to the new device.
1026d560168bSGuenter Roeck  */
1027d560168bSGuenter Roeck struct device *
1028d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name,
1029d560168bSGuenter Roeck 				void *drvdata,
1030d560168bSGuenter Roeck 				const struct hwmon_chip_info *chip,
1031848ba0a2SGuenter Roeck 				const struct attribute_group **extra_groups)
1032d560168bSGuenter Roeck {
1033ddaefa20SGuenter Roeck 	if (!dev || !name || !chip)
10348353863aSGuenter Roeck 		return ERR_PTR(-EINVAL);
10358353863aSGuenter Roeck 
1036ddaefa20SGuenter Roeck 	if (!chip->ops || !chip->ops->is_visible || !chip->info)
103759df4f4eSLucas Magasweran 		return ERR_PTR(-EINVAL);
103859df4f4eSLucas Magasweran 
1039848ba0a2SGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
1040d560168bSGuenter Roeck }
1041d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
1042d560168bSGuenter Roeck 
1043d560168bSGuenter Roeck /**
1044e5d21072SGuenter Roeck  * hwmon_device_register_for_thermal - register hwmon device for thermal subsystem
1045e5d21072SGuenter Roeck  * @dev: the parent device
1046e5d21072SGuenter Roeck  * @name: hwmon name attribute
1047e5d21072SGuenter Roeck  * @drvdata: driver data to attach to created device
1048e5d21072SGuenter Roeck  *
1049e5d21072SGuenter Roeck  * The use of this function is restricted. It is provided for legacy reasons
1050e5d21072SGuenter Roeck  * and must only be called from the thermal subsystem.
1051e5d21072SGuenter Roeck  *
1052e5d21072SGuenter Roeck  * hwmon_device_unregister() must be called when the device is no
1053e5d21072SGuenter Roeck  * longer needed.
1054e5d21072SGuenter Roeck  *
1055e5d21072SGuenter Roeck  * Returns the pointer to the new device.
1056e5d21072SGuenter Roeck  */
1057e5d21072SGuenter Roeck struct device *
1058e5d21072SGuenter Roeck hwmon_device_register_for_thermal(struct device *dev, const char *name,
1059e5d21072SGuenter Roeck 				  void *drvdata)
1060e5d21072SGuenter Roeck {
1061e5d21072SGuenter Roeck 	if (!name || !dev)
1062e5d21072SGuenter Roeck 		return ERR_PTR(-EINVAL);
1063e5d21072SGuenter Roeck 
1064e5d21072SGuenter Roeck 	return __hwmon_device_register(dev, name, drvdata, NULL, NULL);
1065e5d21072SGuenter Roeck }
1066e5d21072SGuenter Roeck EXPORT_SYMBOL_NS_GPL(hwmon_device_register_for_thermal, HWMON_THERMAL);
1067e5d21072SGuenter Roeck 
1068e5d21072SGuenter Roeck /**
10691beeffe4STony Jones  * hwmon_device_register - register w/ hwmon
10701236441fSMark M. Hoffman  * @dev: the device to register
10711236441fSMark M. Hoffman  *
10721beeffe4STony Jones  * hwmon_device_unregister() must be called when the device is no
10731236441fSMark M. Hoffman  * longer needed.
10741236441fSMark M. Hoffman  *
10751beeffe4STony Jones  * Returns the pointer to the new device.
10761236441fSMark M. Hoffman  */
10771beeffe4STony Jones struct device *hwmon_device_register(struct device *dev)
10781236441fSMark M. Hoffman {
1079af1bd36cSGuenter Roeck 	dev_warn(dev,
1080af1bd36cSGuenter Roeck 		 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
1081af1bd36cSGuenter Roeck 
10828353863aSGuenter Roeck 	return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
10831236441fSMark M. Hoffman }
1084839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register);
10851236441fSMark M. Hoffman 
10861236441fSMark M. Hoffman /**
10871236441fSMark M. Hoffman  * hwmon_device_unregister - removes the previously registered class device
10881236441fSMark M. Hoffman  *
10891beeffe4STony Jones  * @dev: the class device to destroy
10901236441fSMark M. Hoffman  */
10911beeffe4STony Jones void hwmon_device_unregister(struct device *dev)
10921236441fSMark M. Hoffman {
10931236441fSMark M. Hoffman 	int id;
10941236441fSMark M. Hoffman 
1095739cf3a2SKay Sievers 	if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
10961beeffe4STony Jones 		device_unregister(dev);
1097718fbfa5Skeliu 		ida_free(&hwmon_ida, id);
10981236441fSMark M. Hoffman 	} else
10991beeffe4STony Jones 		dev_dbg(dev->parent,
11001236441fSMark M. Hoffman 			"hwmon_device_unregister() failed: bad class ID!\n");
11011236441fSMark M. Hoffman }
1102839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister);
11031236441fSMark M. Hoffman 
110474188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res)
110574188cbaSGuenter Roeck {
110674188cbaSGuenter Roeck 	struct device *hwdev = *(struct device **)res;
110774188cbaSGuenter Roeck 
110874188cbaSGuenter Roeck 	hwmon_device_unregister(hwdev);
110974188cbaSGuenter Roeck }
111074188cbaSGuenter Roeck 
111174188cbaSGuenter Roeck /**
111274188cbaSGuenter Roeck  * devm_hwmon_device_register_with_groups - register w/ hwmon
111374188cbaSGuenter Roeck  * @dev: the parent device
111474188cbaSGuenter Roeck  * @name: hwmon name attribute
111574188cbaSGuenter Roeck  * @drvdata: driver data to attach to created device
111674188cbaSGuenter Roeck  * @groups: List of attribute groups to create
111774188cbaSGuenter Roeck  *
111874188cbaSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
111974188cbaSGuenter Roeck  * unregistered with the parent device.
112074188cbaSGuenter Roeck  */
112174188cbaSGuenter Roeck struct device *
112274188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
112374188cbaSGuenter Roeck 				       void *drvdata,
112474188cbaSGuenter Roeck 				       const struct attribute_group **groups)
112574188cbaSGuenter Roeck {
112674188cbaSGuenter Roeck 	struct device **ptr, *hwdev;
112774188cbaSGuenter Roeck 
112874188cbaSGuenter Roeck 	if (!dev)
112974188cbaSGuenter Roeck 		return ERR_PTR(-EINVAL);
113074188cbaSGuenter Roeck 
113174188cbaSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
113274188cbaSGuenter Roeck 	if (!ptr)
113374188cbaSGuenter Roeck 		return ERR_PTR(-ENOMEM);
113474188cbaSGuenter Roeck 
113574188cbaSGuenter Roeck 	hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
113674188cbaSGuenter Roeck 	if (IS_ERR(hwdev))
113774188cbaSGuenter Roeck 		goto error;
113874188cbaSGuenter Roeck 
113974188cbaSGuenter Roeck 	*ptr = hwdev;
114074188cbaSGuenter Roeck 	devres_add(dev, ptr);
114174188cbaSGuenter Roeck 	return hwdev;
114274188cbaSGuenter Roeck 
114374188cbaSGuenter Roeck error:
114474188cbaSGuenter Roeck 	devres_free(ptr);
114574188cbaSGuenter Roeck 	return hwdev;
114674188cbaSGuenter Roeck }
114774188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
114874188cbaSGuenter Roeck 
1149d560168bSGuenter Roeck /**
1150d560168bSGuenter Roeck  * devm_hwmon_device_register_with_info - register w/ hwmon
1151d560168bSGuenter Roeck  * @dev:	the parent device
1152d560168bSGuenter Roeck  * @name:	hwmon name attribute
1153d560168bSGuenter Roeck  * @drvdata:	driver data to attach to created device
11543870945aSGuenter Roeck  * @chip:	pointer to hwmon chip information
11553b9da042SJames Seo  * @extra_groups: pointer to list of driver specific attribute groups
1156d560168bSGuenter Roeck  *
1157d560168bSGuenter Roeck  * Returns the pointer to the new device. The new device is automatically
1158d560168bSGuenter Roeck  * unregistered with the parent device.
1159d560168bSGuenter Roeck  */
1160d560168bSGuenter Roeck struct device *
1161d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name,
1162d560168bSGuenter Roeck 				     void *drvdata,
1163d560168bSGuenter Roeck 				     const struct hwmon_chip_info *chip,
11643b9da042SJames Seo 				     const struct attribute_group **extra_groups)
1165d560168bSGuenter Roeck {
1166d560168bSGuenter Roeck 	struct device **ptr, *hwdev;
1167d560168bSGuenter Roeck 
1168d560168bSGuenter Roeck 	if (!dev)
1169d560168bSGuenter Roeck 		return ERR_PTR(-EINVAL);
1170d560168bSGuenter Roeck 
1171d560168bSGuenter Roeck 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
1172d560168bSGuenter Roeck 	if (!ptr)
1173d560168bSGuenter Roeck 		return ERR_PTR(-ENOMEM);
1174d560168bSGuenter Roeck 
1175d560168bSGuenter Roeck 	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
11763b9da042SJames Seo 						extra_groups);
1177d560168bSGuenter Roeck 	if (IS_ERR(hwdev))
1178d560168bSGuenter Roeck 		goto error;
1179d560168bSGuenter Roeck 
1180d560168bSGuenter Roeck 	*ptr = hwdev;
1181d560168bSGuenter Roeck 	devres_add(dev, ptr);
1182d560168bSGuenter Roeck 
1183d560168bSGuenter Roeck 	return hwdev;
1184d560168bSGuenter Roeck 
1185d560168bSGuenter Roeck error:
1186d560168bSGuenter Roeck 	devres_free(ptr);
1187d560168bSGuenter Roeck 	return hwdev;
1188d560168bSGuenter Roeck }
1189d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
1190d560168bSGuenter Roeck 
119174188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data)
119274188cbaSGuenter Roeck {
119374188cbaSGuenter Roeck 	struct device **hwdev = res;
119474188cbaSGuenter Roeck 
119574188cbaSGuenter Roeck 	return *hwdev == data;
119674188cbaSGuenter Roeck }
119774188cbaSGuenter Roeck 
119874188cbaSGuenter Roeck /**
119974188cbaSGuenter Roeck  * devm_hwmon_device_unregister - removes a previously registered hwmon device
120074188cbaSGuenter Roeck  *
120174188cbaSGuenter Roeck  * @dev: the parent device of the device to unregister
120274188cbaSGuenter Roeck  */
120374188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev)
120474188cbaSGuenter Roeck {
120574188cbaSGuenter Roeck 	WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
120674188cbaSGuenter Roeck }
120774188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
120874188cbaSGuenter Roeck 
12091ad6c3b7SMichael Walle static char *__hwmon_sanitize_name(struct device *dev, const char *old_name)
12101ad6c3b7SMichael Walle {
12111ad6c3b7SMichael Walle 	char *name, *p;
12121ad6c3b7SMichael Walle 
12131ad6c3b7SMichael Walle 	if (dev)
12141ad6c3b7SMichael Walle 		name = devm_kstrdup(dev, old_name, GFP_KERNEL);
12151ad6c3b7SMichael Walle 	else
12161ad6c3b7SMichael Walle 		name = kstrdup(old_name, GFP_KERNEL);
12171ad6c3b7SMichael Walle 	if (!name)
12181ad6c3b7SMichael Walle 		return ERR_PTR(-ENOMEM);
12191ad6c3b7SMichael Walle 
12201ad6c3b7SMichael Walle 	for (p = name; *p; p++)
12211ad6c3b7SMichael Walle 		if (hwmon_is_bad_char(*p))
12221ad6c3b7SMichael Walle 			*p = '_';
12231ad6c3b7SMichael Walle 
12241ad6c3b7SMichael Walle 	return name;
12251ad6c3b7SMichael Walle }
12261ad6c3b7SMichael Walle 
12271ad6c3b7SMichael Walle /**
12281ad6c3b7SMichael Walle  * hwmon_sanitize_name - Replaces invalid characters in a hwmon name
12291ad6c3b7SMichael Walle  * @name: NUL-terminated name
12301ad6c3b7SMichael Walle  *
12311ad6c3b7SMichael Walle  * Allocates a new string where any invalid characters will be replaced
12321ad6c3b7SMichael Walle  * by an underscore. It is the responsibility of the caller to release
12331ad6c3b7SMichael Walle  * the memory.
12341ad6c3b7SMichael Walle  *
12351ad6c3b7SMichael Walle  * Returns newly allocated name, or ERR_PTR on error.
12361ad6c3b7SMichael Walle  */
12371ad6c3b7SMichael Walle char *hwmon_sanitize_name(const char *name)
12381ad6c3b7SMichael Walle {
12391ad6c3b7SMichael Walle 	return __hwmon_sanitize_name(NULL, name);
12401ad6c3b7SMichael Walle }
12411ad6c3b7SMichael Walle EXPORT_SYMBOL_GPL(hwmon_sanitize_name);
12421ad6c3b7SMichael Walle 
12431ad6c3b7SMichael Walle /**
12441ad6c3b7SMichael Walle  * devm_hwmon_sanitize_name - resource managed hwmon_sanitize_name()
12451ad6c3b7SMichael Walle  * @dev: device to allocate memory for
12461ad6c3b7SMichael Walle  * @name: NUL-terminated name
12471ad6c3b7SMichael Walle  *
12481ad6c3b7SMichael Walle  * Allocates a new string where any invalid characters will be replaced
12491ad6c3b7SMichael Walle  * by an underscore.
12501ad6c3b7SMichael Walle  *
12511ad6c3b7SMichael Walle  * Returns newly allocated name, or ERR_PTR on error.
12521ad6c3b7SMichael Walle  */
12531ad6c3b7SMichael Walle char *devm_hwmon_sanitize_name(struct device *dev, const char *name)
12541ad6c3b7SMichael Walle {
12551ad6c3b7SMichael Walle 	if (!dev)
12561ad6c3b7SMichael Walle 		return ERR_PTR(-EINVAL);
12571ad6c3b7SMichael Walle 
12581ad6c3b7SMichael Walle 	return __hwmon_sanitize_name(dev, name);
12591ad6c3b7SMichael Walle }
12601ad6c3b7SMichael Walle EXPORT_SYMBOL_GPL(devm_hwmon_sanitize_name);
12611ad6c3b7SMichael Walle 
12622958b1ecSJean Delvare static void __init hwmon_pci_quirks(void)
12632958b1ecSJean Delvare {
12642958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI
12652958b1ecSJean Delvare 	struct pci_dev *sb;
12662958b1ecSJean Delvare 	u16 base;
12672958b1ecSJean Delvare 	u8 enable;
12682958b1ecSJean Delvare 
12692958b1ecSJean Delvare 	/* Open access to 0x295-0x296 on MSI MS-7031 */
12702958b1ecSJean Delvare 	sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
1271d6dab7ddSJean Delvare 	if (sb) {
1272d6dab7ddSJean Delvare 		if (sb->subsystem_vendor == 0x1462 &&	/* MSI */
1273d6dab7ddSJean Delvare 		    sb->subsystem_device == 0x0031) {	/* MS-7031 */
12742958b1ecSJean Delvare 			pci_read_config_byte(sb, 0x48, &enable);
12752958b1ecSJean Delvare 			pci_read_config_word(sb, 0x64, &base);
12762958b1ecSJean Delvare 
12772958b1ecSJean Delvare 			if (base == 0 && !(enable & BIT(2))) {
12782958b1ecSJean Delvare 				dev_info(&sb->dev,
12792958b1ecSJean Delvare 					 "Opening wide generic port at 0x295\n");
12802958b1ecSJean Delvare 				pci_write_config_word(sb, 0x64, 0x295);
1281d6dab7ddSJean Delvare 				pci_write_config_byte(sb, 0x48,
1282d6dab7ddSJean Delvare 						      enable | BIT(2));
12832958b1ecSJean Delvare 			}
12842958b1ecSJean Delvare 		}
1285d6dab7ddSJean Delvare 		pci_dev_put(sb);
1286d6dab7ddSJean Delvare 	}
12872958b1ecSJean Delvare #endif
12882958b1ecSJean Delvare }
12892958b1ecSJean Delvare 
12901236441fSMark M. Hoffman static int __init hwmon_init(void)
12911236441fSMark M. Hoffman {
1292bab2243cSGuenter Roeck 	int err;
1293bab2243cSGuenter Roeck 
12942958b1ecSJean Delvare 	hwmon_pci_quirks();
12952958b1ecSJean Delvare 
1296bab2243cSGuenter Roeck 	err = class_register(&hwmon_class);
1297bab2243cSGuenter Roeck 	if (err) {
1298bab2243cSGuenter Roeck 		pr_err("couldn't register hwmon sysfs class\n");
1299bab2243cSGuenter Roeck 		return err;
13001236441fSMark M. Hoffman 	}
13011236441fSMark M. Hoffman 	return 0;
13021236441fSMark M. Hoffman }
13031236441fSMark M. Hoffman 
13041236441fSMark M. Hoffman static void __exit hwmon_exit(void)
13051236441fSMark M. Hoffman {
1306bab2243cSGuenter Roeck 	class_unregister(&hwmon_class);
13071236441fSMark M. Hoffman }
13081236441fSMark M. Hoffman 
130937f54ee5SDavid Brownell subsys_initcall(hwmon_init);
13101236441fSMark M. Hoffman module_exit(hwmon_exit);
13111236441fSMark M. Hoffman 
13121236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
13131236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
13141236441fSMark M. Hoffman MODULE_LICENSE("GPL");
13151236441fSMark M. Hoffman 
1316