Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2    hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
  3
  4    This file defines the sysfs class "hwmon", for use by sensors drivers.
  5
  6    Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
  7
  8    This program is free software; you can redistribute it and/or modify
  9    it under the terms of the GNU General Public License as published by
 10    the Free Software Foundation; version 2 of the License.
 11*/
 12
 13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 14
 15#include <linux/module.h>
 16#include <linux/device.h>
 17#include <linux/err.h>
 18#include <linux/kdev_t.h>
 19#include <linux/idr.h>
 20#include <linux/hwmon.h>
 21#include <linux/gfp.h>
 22#include <linux/spinlock.h>
 
 
 23#include <linux/pci.h>
 
 
 
 
 
 
 24
 25#define HWMON_ID_PREFIX "hwmon"
 26#define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
 27
 28static struct class *hwmon_class;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 29
 30static DEFINE_IDR(hwmon_idr);
 31static DEFINE_SPINLOCK(idr_lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 32
 33/**
 34 * hwmon_device_register - register w/ hwmon
 35 * @dev: the device to register
 36 *
 37 * hwmon_device_unregister() must be called when the device is no
 38 * longer needed.
 39 *
 40 * Returns the pointer to the new device.
 41 */
 42struct device *hwmon_device_register(struct device *dev)
 43{
 44	struct device *hwdev;
 45	int id, err;
 46
 47again:
 48	if (unlikely(idr_pre_get(&hwmon_idr, GFP_KERNEL) == 0))
 49		return ERR_PTR(-ENOMEM);
 50
 51	spin_lock(&idr_lock);
 52	err = idr_get_new(&hwmon_idr, NULL, &id);
 53	spin_unlock(&idr_lock);
 54
 55	if (unlikely(err == -EAGAIN))
 56		goto again;
 57	else if (unlikely(err))
 58		return ERR_PTR(err);
 59
 60	id = id & MAX_ID_MASK;
 61	hwdev = device_create(hwmon_class, dev, MKDEV(0, 0), NULL,
 62			      HWMON_ID_FORMAT, id);
 63
 64	if (IS_ERR(hwdev)) {
 65		spin_lock(&idr_lock);
 66		idr_remove(&hwmon_idr, id);
 67		spin_unlock(&idr_lock);
 68	}
 69
 70	return hwdev;
 71}
 
 72
 73/**
 74 * hwmon_device_unregister - removes the previously registered class device
 75 *
 76 * @dev: the class device to destroy
 77 */
 78void hwmon_device_unregister(struct device *dev)
 79{
 80	int id;
 81
 82	if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
 83		device_unregister(dev);
 84		spin_lock(&idr_lock);
 85		idr_remove(&hwmon_idr, id);
 86		spin_unlock(&idr_lock);
 87	} else
 88		dev_dbg(dev->parent,
 89			"hwmon_device_unregister() failed: bad class ID!\n");
 90}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 91
 92static void __init hwmon_pci_quirks(void)
 93{
 94#if defined CONFIG_X86 && defined CONFIG_PCI
 95	struct pci_dev *sb;
 96	u16 base;
 97	u8 enable;
 98
 99	/* Open access to 0x295-0x296 on MSI MS-7031 */
100	sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
101	if (sb &&
102	    (sb->subsystem_vendor == 0x1462 &&	/* MSI */
103	     sb->subsystem_device == 0x0031)) {	/* MS-7031 */
104
105		pci_read_config_byte(sb, 0x48, &enable);
106		pci_read_config_word(sb, 0x64, &base);
107
108		if (base == 0 && !(enable & BIT(2))) {
109			dev_info(&sb->dev,
110				 "Opening wide generic port at 0x295\n");
111			pci_write_config_word(sb, 0x64, 0x295);
112			pci_write_config_byte(sb, 0x48, enable | BIT(2));
 
113		}
 
114	}
115#endif
116}
117
118static int __init hwmon_init(void)
119{
 
 
120	hwmon_pci_quirks();
121
122	hwmon_class = class_create(THIS_MODULE, "hwmon");
123	if (IS_ERR(hwmon_class)) {
124		pr_err("couldn't create sysfs class\n");
125		return PTR_ERR(hwmon_class);
126	}
127	return 0;
128}
129
130static void __exit hwmon_exit(void)
131{
132	class_destroy(hwmon_class);
133}
134
135subsys_initcall(hwmon_init);
136module_exit(hwmon_exit);
137
138EXPORT_SYMBOL_GPL(hwmon_device_register);
139EXPORT_SYMBOL_GPL(hwmon_device_unregister);
140
141MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
142MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
143MODULE_LICENSE("GPL");
144
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
  4 *
  5 * This file defines the sysfs class "hwmon", for use by sensors drivers.
  6 *
  7 * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
  8 */
 
 
 
 
  9
 10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 11
 12#include <linux/bitops.h>
 13#include <linux/device.h>
 14#include <linux/err.h>
 
 
 
 15#include <linux/gfp.h>
 16#include <linux/hwmon.h>
 17#include <linux/idr.h>
 18#include <linux/module.h>
 19#include <linux/pci.h>
 20#include <linux/slab.h>
 21#include <linux/string.h>
 22#include <linux/thermal.h>
 23
 24#define CREATE_TRACE_POINTS
 25#include <trace/events/hwmon.h>
 26
 27#define HWMON_ID_PREFIX "hwmon"
 28#define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
 29
 30struct hwmon_device {
 31	const char *name;
 32	struct device dev;
 33	const struct hwmon_chip_info *chip;
 34
 35	struct attribute_group group;
 36	const struct attribute_group **groups;
 37};
 38
 39#define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
 40
 41#define MAX_SYSFS_ATTR_NAME_LENGTH	32
 42
 43struct hwmon_device_attribute {
 44	struct device_attribute dev_attr;
 45	const struct hwmon_ops *ops;
 46	enum hwmon_sensor_types type;
 47	u32 attr;
 48	int index;
 49	char name[MAX_SYSFS_ATTR_NAME_LENGTH];
 50};
 51
 52#define to_hwmon_attr(d) \
 53	container_of(d, struct hwmon_device_attribute, dev_attr)
 54
 55/*
 56 * Thermal zone information
 57 * In addition to the reference to the hwmon device,
 58 * also provides the sensor index.
 59 */
 60struct hwmon_thermal_data {
 61	struct hwmon_device *hwdev;	/* Reference to hwmon device */
 62	int index;			/* sensor index */
 63};
 64
 65static ssize_t
 66name_show(struct device *dev, struct device_attribute *attr, char *buf)
 67{
 68	return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
 69}
 70static DEVICE_ATTR_RO(name);
 71
 72static struct attribute *hwmon_dev_attrs[] = {
 73	&dev_attr_name.attr,
 74	NULL
 75};
 76
 77static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
 78					 struct attribute *attr, int n)
 79{
 80	struct device *dev = container_of(kobj, struct device, kobj);
 81
 82	if (to_hwmon_device(dev)->name == NULL)
 83		return 0;
 84
 85	return attr->mode;
 86}
 87
 88static const struct attribute_group hwmon_dev_attr_group = {
 89	.attrs		= hwmon_dev_attrs,
 90	.is_visible	= hwmon_dev_name_is_visible,
 91};
 92
 93static const struct attribute_group *hwmon_dev_attr_groups[] = {
 94	&hwmon_dev_attr_group,
 95	NULL
 96};
 97
 98static void hwmon_dev_release(struct device *dev)
 99{
100	kfree(to_hwmon_device(dev));
101}
102
103static struct class hwmon_class = {
104	.name = "hwmon",
105	.owner = THIS_MODULE,
106	.dev_groups = hwmon_dev_attr_groups,
107	.dev_release = hwmon_dev_release,
108};
109
110static DEFINE_IDA(hwmon_ida);
111
112/* Thermal zone handling */
113
114/*
115 * The complex conditional is necessary to avoid a cyclic dependency
116 * between hwmon and thermal_sys modules.
117 */
118#ifdef CONFIG_THERMAL_OF
119static int hwmon_thermal_get_temp(void *data, int *temp)
120{
121	struct hwmon_thermal_data *tdata = data;
122	struct hwmon_device *hwdev = tdata->hwdev;
123	int ret;
124	long t;
125
126	ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
127				     tdata->index, &t);
128	if (ret < 0)
129		return ret;
130
131	*temp = t;
132
133	return 0;
134}
135
136static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
137	.get_temp = hwmon_thermal_get_temp,
138};
139
140static int hwmon_thermal_add_sensor(struct device *dev,
141				    struct hwmon_device *hwdev, int index)
142{
143	struct hwmon_thermal_data *tdata;
144	struct thermal_zone_device *tzd;
145
146	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
147	if (!tdata)
148		return -ENOMEM;
149
150	tdata->hwdev = hwdev;
151	tdata->index = index;
152
153	tzd = devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
154						   &hwmon_thermal_ops);
155	/*
156	 * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV,
157	 * so ignore that error but forward any other error.
158	 */
159	if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV))
160		return PTR_ERR(tzd);
161
162	return 0;
163}
164#else
165static int hwmon_thermal_add_sensor(struct device *dev,
166				    struct hwmon_device *hwdev, int index)
167{
168	return 0;
169}
170#endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
171
172static int hwmon_attr_base(enum hwmon_sensor_types type)
173{
174	if (type == hwmon_in)
175		return 0;
176	return 1;
177}
178
179/* sysfs attribute management */
180
181static ssize_t hwmon_attr_show(struct device *dev,
182			       struct device_attribute *devattr, char *buf)
183{
184	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
185	long val;
186	int ret;
187
188	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
189			       &val);
190	if (ret < 0)
191		return ret;
192
193	trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
194			      hattr->name, val);
195
196	return sprintf(buf, "%ld\n", val);
197}
198
199static ssize_t hwmon_attr_show_string(struct device *dev,
200				      struct device_attribute *devattr,
201				      char *buf)
202{
203	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
204	enum hwmon_sensor_types type = hattr->type;
205	const char *s;
206	int ret;
207
208	ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
209				      hattr->index, &s);
210	if (ret < 0)
211		return ret;
212
213	trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
214				     hattr->name, s);
215
216	return sprintf(buf, "%s\n", s);
217}
218
219static ssize_t hwmon_attr_store(struct device *dev,
220				struct device_attribute *devattr,
221				const char *buf, size_t count)
222{
223	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
224	long val;
225	int ret;
226
227	ret = kstrtol(buf, 10, &val);
228	if (ret < 0)
229		return ret;
230
231	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
232				val);
233	if (ret < 0)
234		return ret;
235
236	trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
237			       hattr->name, val);
238
239	return count;
240}
241
242static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
243{
244	return (type == hwmon_temp && attr == hwmon_temp_label) ||
245	       (type == hwmon_in && attr == hwmon_in_label) ||
246	       (type == hwmon_curr && attr == hwmon_curr_label) ||
247	       (type == hwmon_power && attr == hwmon_power_label) ||
248	       (type == hwmon_energy && attr == hwmon_energy_label) ||
249	       (type == hwmon_humidity && attr == hwmon_humidity_label) ||
250	       (type == hwmon_fan && attr == hwmon_fan_label);
251}
252
253static struct attribute *hwmon_genattr(struct device *dev,
254				       const void *drvdata,
255				       enum hwmon_sensor_types type,
256				       u32 attr,
257				       int index,
258				       const char *template,
259				       const struct hwmon_ops *ops)
260{
261	struct hwmon_device_attribute *hattr;
262	struct device_attribute *dattr;
263	struct attribute *a;
264	umode_t mode;
265	const char *name;
266	bool is_string = is_string_attr(type, attr);
267
268	/* The attribute is invisible if there is no template string */
269	if (!template)
270		return ERR_PTR(-ENOENT);
271
272	mode = ops->is_visible(drvdata, type, attr, index);
273	if (!mode)
274		return ERR_PTR(-ENOENT);
275
276	if ((mode & 0444) && ((is_string && !ops->read_string) ||
277				 (!is_string && !ops->read)))
278		return ERR_PTR(-EINVAL);
279	if ((mode & 0222) && !ops->write)
280		return ERR_PTR(-EINVAL);
281
282	hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
283	if (!hattr)
284		return ERR_PTR(-ENOMEM);
285
286	if (type == hwmon_chip) {
287		name = template;
288	} else {
289		scnprintf(hattr->name, sizeof(hattr->name), template,
290			  index + hwmon_attr_base(type));
291		name = hattr->name;
292	}
293
294	hattr->type = type;
295	hattr->attr = attr;
296	hattr->index = index;
297	hattr->ops = ops;
298
299	dattr = &hattr->dev_attr;
300	dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
301	dattr->store = hwmon_attr_store;
302
303	a = &dattr->attr;
304	sysfs_attr_init(a);
305	a->name = name;
306	a->mode = mode;
307
308	return a;
309}
310
311/*
312 * Chip attributes are not attribute templates but actual sysfs attributes.
313 * See hwmon_genattr() for special handling.
314 */
315static const char * const hwmon_chip_attrs[] = {
316	[hwmon_chip_temp_reset_history] = "temp_reset_history",
317	[hwmon_chip_in_reset_history] = "in_reset_history",
318	[hwmon_chip_curr_reset_history] = "curr_reset_history",
319	[hwmon_chip_power_reset_history] = "power_reset_history",
320	[hwmon_chip_update_interval] = "update_interval",
321	[hwmon_chip_alarms] = "alarms",
322	[hwmon_chip_samples] = "samples",
323	[hwmon_chip_curr_samples] = "curr_samples",
324	[hwmon_chip_in_samples] = "in_samples",
325	[hwmon_chip_power_samples] = "power_samples",
326	[hwmon_chip_temp_samples] = "temp_samples",
327};
328
329static const char * const hwmon_temp_attr_templates[] = {
330	[hwmon_temp_input] = "temp%d_input",
331	[hwmon_temp_type] = "temp%d_type",
332	[hwmon_temp_lcrit] = "temp%d_lcrit",
333	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
334	[hwmon_temp_min] = "temp%d_min",
335	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
336	[hwmon_temp_max] = "temp%d_max",
337	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
338	[hwmon_temp_crit] = "temp%d_crit",
339	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
340	[hwmon_temp_emergency] = "temp%d_emergency",
341	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
342	[hwmon_temp_alarm] = "temp%d_alarm",
343	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
344	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
345	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
346	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
347	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
348	[hwmon_temp_fault] = "temp%d_fault",
349	[hwmon_temp_offset] = "temp%d_offset",
350	[hwmon_temp_label] = "temp%d_label",
351	[hwmon_temp_lowest] = "temp%d_lowest",
352	[hwmon_temp_highest] = "temp%d_highest",
353	[hwmon_temp_reset_history] = "temp%d_reset_history",
354};
355
356static const char * const hwmon_in_attr_templates[] = {
357	[hwmon_in_input] = "in%d_input",
358	[hwmon_in_min] = "in%d_min",
359	[hwmon_in_max] = "in%d_max",
360	[hwmon_in_lcrit] = "in%d_lcrit",
361	[hwmon_in_crit] = "in%d_crit",
362	[hwmon_in_average] = "in%d_average",
363	[hwmon_in_lowest] = "in%d_lowest",
364	[hwmon_in_highest] = "in%d_highest",
365	[hwmon_in_reset_history] = "in%d_reset_history",
366	[hwmon_in_label] = "in%d_label",
367	[hwmon_in_alarm] = "in%d_alarm",
368	[hwmon_in_min_alarm] = "in%d_min_alarm",
369	[hwmon_in_max_alarm] = "in%d_max_alarm",
370	[hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
371	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
372	[hwmon_in_enable] = "in%d_enable",
373};
374
375static const char * const hwmon_curr_attr_templates[] = {
376	[hwmon_curr_input] = "curr%d_input",
377	[hwmon_curr_min] = "curr%d_min",
378	[hwmon_curr_max] = "curr%d_max",
379	[hwmon_curr_lcrit] = "curr%d_lcrit",
380	[hwmon_curr_crit] = "curr%d_crit",
381	[hwmon_curr_average] = "curr%d_average",
382	[hwmon_curr_lowest] = "curr%d_lowest",
383	[hwmon_curr_highest] = "curr%d_highest",
384	[hwmon_curr_reset_history] = "curr%d_reset_history",
385	[hwmon_curr_label] = "curr%d_label",
386	[hwmon_curr_alarm] = "curr%d_alarm",
387	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
388	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
389	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
390	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
391};
392
393static const char * const hwmon_power_attr_templates[] = {
394	[hwmon_power_average] = "power%d_average",
395	[hwmon_power_average_interval] = "power%d_average_interval",
396	[hwmon_power_average_interval_max] = "power%d_interval_max",
397	[hwmon_power_average_interval_min] = "power%d_interval_min",
398	[hwmon_power_average_highest] = "power%d_average_highest",
399	[hwmon_power_average_lowest] = "power%d_average_lowest",
400	[hwmon_power_average_max] = "power%d_average_max",
401	[hwmon_power_average_min] = "power%d_average_min",
402	[hwmon_power_input] = "power%d_input",
403	[hwmon_power_input_highest] = "power%d_input_highest",
404	[hwmon_power_input_lowest] = "power%d_input_lowest",
405	[hwmon_power_reset_history] = "power%d_reset_history",
406	[hwmon_power_accuracy] = "power%d_accuracy",
407	[hwmon_power_cap] = "power%d_cap",
408	[hwmon_power_cap_hyst] = "power%d_cap_hyst",
409	[hwmon_power_cap_max] = "power%d_cap_max",
410	[hwmon_power_cap_min] = "power%d_cap_min",
411	[hwmon_power_min] = "power%d_min",
412	[hwmon_power_max] = "power%d_max",
413	[hwmon_power_lcrit] = "power%d_lcrit",
414	[hwmon_power_crit] = "power%d_crit",
415	[hwmon_power_label] = "power%d_label",
416	[hwmon_power_alarm] = "power%d_alarm",
417	[hwmon_power_cap_alarm] = "power%d_cap_alarm",
418	[hwmon_power_min_alarm] = "power%d_min_alarm",
419	[hwmon_power_max_alarm] = "power%d_max_alarm",
420	[hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
421	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
422};
423
424static const char * const hwmon_energy_attr_templates[] = {
425	[hwmon_energy_input] = "energy%d_input",
426	[hwmon_energy_label] = "energy%d_label",
427};
428
429static const char * const hwmon_humidity_attr_templates[] = {
430	[hwmon_humidity_input] = "humidity%d_input",
431	[hwmon_humidity_label] = "humidity%d_label",
432	[hwmon_humidity_min] = "humidity%d_min",
433	[hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
434	[hwmon_humidity_max] = "humidity%d_max",
435	[hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
436	[hwmon_humidity_alarm] = "humidity%d_alarm",
437	[hwmon_humidity_fault] = "humidity%d_fault",
438};
439
440static const char * const hwmon_fan_attr_templates[] = {
441	[hwmon_fan_input] = "fan%d_input",
442	[hwmon_fan_label] = "fan%d_label",
443	[hwmon_fan_min] = "fan%d_min",
444	[hwmon_fan_max] = "fan%d_max",
445	[hwmon_fan_div] = "fan%d_div",
446	[hwmon_fan_pulses] = "fan%d_pulses",
447	[hwmon_fan_target] = "fan%d_target",
448	[hwmon_fan_alarm] = "fan%d_alarm",
449	[hwmon_fan_min_alarm] = "fan%d_min_alarm",
450	[hwmon_fan_max_alarm] = "fan%d_max_alarm",
451	[hwmon_fan_fault] = "fan%d_fault",
452};
453
454static const char * const hwmon_pwm_attr_templates[] = {
455	[hwmon_pwm_input] = "pwm%d",
456	[hwmon_pwm_enable] = "pwm%d_enable",
457	[hwmon_pwm_mode] = "pwm%d_mode",
458	[hwmon_pwm_freq] = "pwm%d_freq",
459};
460
461static const char * const *__templates[] = {
462	[hwmon_chip] = hwmon_chip_attrs,
463	[hwmon_temp] = hwmon_temp_attr_templates,
464	[hwmon_in] = hwmon_in_attr_templates,
465	[hwmon_curr] = hwmon_curr_attr_templates,
466	[hwmon_power] = hwmon_power_attr_templates,
467	[hwmon_energy] = hwmon_energy_attr_templates,
468	[hwmon_humidity] = hwmon_humidity_attr_templates,
469	[hwmon_fan] = hwmon_fan_attr_templates,
470	[hwmon_pwm] = hwmon_pwm_attr_templates,
471};
472
473static const int __templates_size[] = {
474	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
475	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
476	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
477	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
478	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
479	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
480	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
481	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
482	[hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
483};
484
485static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
486{
487	int i, n;
488
489	for (i = n = 0; info->config[i]; i++)
490		n += hweight32(info->config[i]);
491
492	return n;
493}
494
495static int hwmon_genattrs(struct device *dev,
496			  const void *drvdata,
497			  struct attribute **attrs,
498			  const struct hwmon_ops *ops,
499			  const struct hwmon_channel_info *info)
500{
501	const char * const *templates;
502	int template_size;
503	int i, aindex = 0;
504
505	if (info->type >= ARRAY_SIZE(__templates))
506		return -EINVAL;
507
508	templates = __templates[info->type];
509	template_size = __templates_size[info->type];
510
511	for (i = 0; info->config[i]; i++) {
512		u32 attr_mask = info->config[i];
513		u32 attr;
514
515		while (attr_mask) {
516			struct attribute *a;
517
518			attr = __ffs(attr_mask);
519			attr_mask &= ~BIT(attr);
520			if (attr >= template_size)
521				return -EINVAL;
522			a = hwmon_genattr(dev, drvdata, info->type, attr, i,
523					  templates[attr], ops);
524			if (IS_ERR(a)) {
525				if (PTR_ERR(a) != -ENOENT)
526					return PTR_ERR(a);
527				continue;
528			}
529			attrs[aindex++] = a;
530		}
531	}
532	return aindex;
533}
534
535static struct attribute **
536__hwmon_create_attrs(struct device *dev, const void *drvdata,
537		     const struct hwmon_chip_info *chip)
538{
539	int ret, i, aindex = 0, nattrs = 0;
540	struct attribute **attrs;
541
542	for (i = 0; chip->info[i]; i++)
543		nattrs += hwmon_num_channel_attrs(chip->info[i]);
544
545	if (nattrs == 0)
546		return ERR_PTR(-EINVAL);
547
548	attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
549	if (!attrs)
550		return ERR_PTR(-ENOMEM);
551
552	for (i = 0; chip->info[i]; i++) {
553		ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
554				     chip->info[i]);
555		if (ret < 0)
556			return ERR_PTR(ret);
557		aindex += ret;
558	}
559
560	return attrs;
561}
562
563static struct device *
564__hwmon_device_register(struct device *dev, const char *name, void *drvdata,
565			const struct hwmon_chip_info *chip,
566			const struct attribute_group **groups)
567{
568	struct hwmon_device *hwdev;
569	struct device *hdev;
570	int i, j, err, id;
571
572	/* Complain about invalid characters in hwmon name attribute */
573	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
574		dev_warn(dev,
575			 "hwmon: '%s' is not a valid name attribute, please fix\n",
576			 name);
577
578	id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
579	if (id < 0)
580		return ERR_PTR(id);
581
582	hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
583	if (hwdev == NULL) {
584		err = -ENOMEM;
585		goto ida_remove;
586	}
587
588	hdev = &hwdev->dev;
589
590	if (chip) {
591		struct attribute **attrs;
592		int ngroups = 2; /* terminating NULL plus &hwdev->groups */
593
594		if (groups)
595			for (i = 0; groups[i]; i++)
596				ngroups++;
597
598		hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
599					     GFP_KERNEL);
600		if (!hwdev->groups) {
601			err = -ENOMEM;
602			goto free_hwmon;
603		}
604
605		attrs = __hwmon_create_attrs(dev, drvdata, chip);
606		if (IS_ERR(attrs)) {
607			err = PTR_ERR(attrs);
608			goto free_hwmon;
609		}
610
611		hwdev->group.attrs = attrs;
612		ngroups = 0;
613		hwdev->groups[ngroups++] = &hwdev->group;
614
615		if (groups) {
616			for (i = 0; groups[i]; i++)
617				hwdev->groups[ngroups++] = groups[i];
618		}
619
620		hdev->groups = hwdev->groups;
621	} else {
622		hdev->groups = groups;
623	}
624
625	hwdev->name = name;
626	hdev->class = &hwmon_class;
627	hdev->parent = dev;
628	hdev->of_node = dev ? dev->of_node : NULL;
629	hwdev->chip = chip;
630	dev_set_drvdata(hdev, drvdata);
631	dev_set_name(hdev, HWMON_ID_FORMAT, id);
632	err = device_register(hdev);
633	if (err)
634		goto free_hwmon;
635
636	if (dev && dev->of_node && chip && chip->ops->read &&
637	    chip->info[0]->type == hwmon_chip &&
638	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
639		const struct hwmon_channel_info **info = chip->info;
640
641		for (i = 1; info[i]; i++) {
642			if (info[i]->type != hwmon_temp)
643				continue;
644
645			for (j = 0; info[i]->config[j]; j++) {
646				if (!chip->ops->is_visible(drvdata, hwmon_temp,
647							   hwmon_temp_input, j))
648					continue;
649				if (info[i]->config[j] & HWMON_T_INPUT) {
650					err = hwmon_thermal_add_sensor(dev,
651								hwdev, j);
652					if (err) {
653						device_unregister(hdev);
654						/*
655						 * Don't worry about hwdev;
656						 * hwmon_dev_release(), called
657						 * from device_unregister(),
658						 * will free it.
659						 */
660						goto ida_remove;
661					}
662				}
663			}
664		}
665	}
666
667	return hdev;
668
669free_hwmon:
670	kfree(hwdev);
671ida_remove:
672	ida_simple_remove(&hwmon_ida, id);
673	return ERR_PTR(err);
674}
675
676/**
677 * hwmon_device_register_with_groups - register w/ hwmon
678 * @dev: the parent device
679 * @name: hwmon name attribute
680 * @drvdata: driver data to attach to created device
681 * @groups: List of attribute groups to create
682 *
683 * hwmon_device_unregister() must be called when the device is no
684 * longer needed.
685 *
686 * Returns the pointer to the new device.
687 */
688struct device *
689hwmon_device_register_with_groups(struct device *dev, const char *name,
690				  void *drvdata,
691				  const struct attribute_group **groups)
692{
693	if (!name)
694		return ERR_PTR(-EINVAL);
695
696	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
697}
698EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
699
700/**
701 * hwmon_device_register_with_info - register w/ hwmon
702 * @dev: the parent device
703 * @name: hwmon name attribute
704 * @drvdata: driver data to attach to created device
705 * @chip: pointer to hwmon chip information
706 * @extra_groups: pointer to list of additional non-standard attribute groups
707 *
708 * hwmon_device_unregister() must be called when the device is no
709 * longer needed.
710 *
711 * Returns the pointer to the new device.
712 */
713struct device *
714hwmon_device_register_with_info(struct device *dev, const char *name,
715				void *drvdata,
716				const struct hwmon_chip_info *chip,
717				const struct attribute_group **extra_groups)
718{
719	if (!name)
720		return ERR_PTR(-EINVAL);
721
722	if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
723		return ERR_PTR(-EINVAL);
724
725	if (chip && !dev)
726		return ERR_PTR(-EINVAL);
727
728	return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
729}
730EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
731
732/**
733 * hwmon_device_register - register w/ hwmon
734 * @dev: the device to register
735 *
736 * hwmon_device_unregister() must be called when the device is no
737 * longer needed.
738 *
739 * Returns the pointer to the new device.
740 */
741struct device *hwmon_device_register(struct device *dev)
742{
743	dev_warn(dev,
744		 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
745
746	return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
747}
748EXPORT_SYMBOL_GPL(hwmon_device_register);
749
750/**
751 * hwmon_device_unregister - removes the previously registered class device
752 *
753 * @dev: the class device to destroy
754 */
755void hwmon_device_unregister(struct device *dev)
756{
757	int id;
758
759	if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
760		device_unregister(dev);
761		ida_simple_remove(&hwmon_ida, id);
 
 
762	} else
763		dev_dbg(dev->parent,
764			"hwmon_device_unregister() failed: bad class ID!\n");
765}
766EXPORT_SYMBOL_GPL(hwmon_device_unregister);
767
768static void devm_hwmon_release(struct device *dev, void *res)
769{
770	struct device *hwdev = *(struct device **)res;
771
772	hwmon_device_unregister(hwdev);
773}
774
775/**
776 * devm_hwmon_device_register_with_groups - register w/ hwmon
777 * @dev: the parent device
778 * @name: hwmon name attribute
779 * @drvdata: driver data to attach to created device
780 * @groups: List of attribute groups to create
781 *
782 * Returns the pointer to the new device. The new device is automatically
783 * unregistered with the parent device.
784 */
785struct device *
786devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
787				       void *drvdata,
788				       const struct attribute_group **groups)
789{
790	struct device **ptr, *hwdev;
791
792	if (!dev)
793		return ERR_PTR(-EINVAL);
794
795	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
796	if (!ptr)
797		return ERR_PTR(-ENOMEM);
798
799	hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
800	if (IS_ERR(hwdev))
801		goto error;
802
803	*ptr = hwdev;
804	devres_add(dev, ptr);
805	return hwdev;
806
807error:
808	devres_free(ptr);
809	return hwdev;
810}
811EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
812
813/**
814 * devm_hwmon_device_register_with_info - register w/ hwmon
815 * @dev:	the parent device
816 * @name:	hwmon name attribute
817 * @drvdata:	driver data to attach to created device
818 * @chip:	pointer to hwmon chip information
819 * @groups:	pointer to list of driver specific attribute groups
820 *
821 * Returns the pointer to the new device. The new device is automatically
822 * unregistered with the parent device.
823 */
824struct device *
825devm_hwmon_device_register_with_info(struct device *dev, const char *name,
826				     void *drvdata,
827				     const struct hwmon_chip_info *chip,
828				     const struct attribute_group **groups)
829{
830	struct device **ptr, *hwdev;
831
832	if (!dev)
833		return ERR_PTR(-EINVAL);
834
835	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
836	if (!ptr)
837		return ERR_PTR(-ENOMEM);
838
839	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
840						groups);
841	if (IS_ERR(hwdev))
842		goto error;
843
844	*ptr = hwdev;
845	devres_add(dev, ptr);
846
847	return hwdev;
848
849error:
850	devres_free(ptr);
851	return hwdev;
852}
853EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
854
855static int devm_hwmon_match(struct device *dev, void *res, void *data)
856{
857	struct device **hwdev = res;
858
859	return *hwdev == data;
860}
861
862/**
863 * devm_hwmon_device_unregister - removes a previously registered hwmon device
864 *
865 * @dev: the parent device of the device to unregister
866 */
867void devm_hwmon_device_unregister(struct device *dev)
868{
869	WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
870}
871EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
872
873static void __init hwmon_pci_quirks(void)
874{
875#if defined CONFIG_X86 && defined CONFIG_PCI
876	struct pci_dev *sb;
877	u16 base;
878	u8 enable;
879
880	/* Open access to 0x295-0x296 on MSI MS-7031 */
881	sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
882	if (sb) {
883		if (sb->subsystem_vendor == 0x1462 &&	/* MSI */
884		    sb->subsystem_device == 0x0031) {	/* MS-7031 */
885			pci_read_config_byte(sb, 0x48, &enable);
886			pci_read_config_word(sb, 0x64, &base);
887
888			if (base == 0 && !(enable & BIT(2))) {
889				dev_info(&sb->dev,
890					 "Opening wide generic port at 0x295\n");
891				pci_write_config_word(sb, 0x64, 0x295);
892				pci_write_config_byte(sb, 0x48,
893						      enable | BIT(2));
894			}
895		}
896		pci_dev_put(sb);
897	}
898#endif
899}
900
901static int __init hwmon_init(void)
902{
903	int err;
904
905	hwmon_pci_quirks();
906
907	err = class_register(&hwmon_class);
908	if (err) {
909		pr_err("couldn't register hwmon sysfs class\n");
910		return err;
911	}
912	return 0;
913}
914
915static void __exit hwmon_exit(void)
916{
917	class_unregister(&hwmon_class);
918}
919
920subsys_initcall(hwmon_init);
921module_exit(hwmon_exit);
 
 
 
922
923MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
924MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
925MODULE_LICENSE("GPL");
926