blob: 649a68d119b4647121a35357bb7233b194b708eb [file] [log] [blame]
Mark M. Hoffman12364412005-07-15 21:38:08 -04001/*
Guenter Roeck5ed04882012-01-19 11:02:18 -08002 * 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 */
Mark M. Hoffman12364412005-07-15 21:38:08 -040012
Joe Perchesc95df1a2010-10-20 06:51:37 +000013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Mark M. Hoffman12364412005-07-15 21:38:08 -040015#include <linux/device.h>
16#include <linux/err.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080017#include <linux/gfp.h>
Guenter Roeckc9ebbe62016-07-10 09:43:21 -070018#include <linux/hwmon.h>
19#include <linux/idr.h>
20#include <linux/module.h>
Jean Delvare2958b1e2009-06-15 18:39:50 +020021#include <linux/pci.h>
Guenter Roeckc9ebbe62016-07-10 09:43:21 -070022#include <linux/slab.h>
Guenter Roeck648cd482014-02-28 10:37:55 -080023#include <linux/string.h>
Mark M. Hoffman12364412005-07-15 21:38:08 -040024
25#define HWMON_ID_PREFIX "hwmon"
26#define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
27
Guenter Roeckbab22432013-07-06 13:57:23 -070028struct hwmon_device {
29 const char *name;
30 struct device dev;
31};
32#define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
33
34static ssize_t
35show_name(struct device *dev, struct device_attribute *attr, char *buf)
36{
37 return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
38}
39static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
40
41static struct attribute *hwmon_dev_attrs[] = {
42 &dev_attr_name.attr,
43 NULL
44};
45
46static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
47 struct attribute *attr, int n)
48{
49 struct device *dev = container_of(kobj, struct device, kobj);
50
51 if (to_hwmon_device(dev)->name == NULL)
52 return 0;
53
54 return attr->mode;
55}
56
57static struct attribute_group hwmon_dev_attr_group = {
58 .attrs = hwmon_dev_attrs,
59 .is_visible = hwmon_dev_name_is_visible,
60};
61
62static const struct attribute_group *hwmon_dev_attr_groups[] = {
63 &hwmon_dev_attr_group,
64 NULL
65};
66
67static void hwmon_dev_release(struct device *dev)
68{
69 kfree(to_hwmon_device(dev));
70}
71
72static struct class hwmon_class = {
73 .name = "hwmon",
74 .owner = THIS_MODULE,
75 .dev_groups = hwmon_dev_attr_groups,
76 .dev_release = hwmon_dev_release,
77};
Mark M. Hoffman12364412005-07-15 21:38:08 -040078
Jonathan Cameron4ca5f462011-10-31 17:10:09 -070079static DEFINE_IDA(hwmon_ida);
Mark M. Hoffman12364412005-07-15 21:38:08 -040080
81/**
Guenter Roeckbab22432013-07-06 13:57:23 -070082 * hwmon_device_register_with_groups - register w/ hwmon
83 * @dev: the parent device
84 * @name: hwmon name attribute
85 * @drvdata: driver data to attach to created device
86 * @groups: List of attribute groups to create
87 *
88 * hwmon_device_unregister() must be called when the device is no
89 * longer needed.
90 *
91 * Returns the pointer to the new device.
92 */
93struct device *
94hwmon_device_register_with_groups(struct device *dev, const char *name,
95 void *drvdata,
96 const struct attribute_group **groups)
97{
98 struct hwmon_device *hwdev;
99 int err, id;
100
Guenter Roeck648cd482014-02-28 10:37:55 -0800101 /* Do not accept invalid characters in hwmon name attribute */
102 if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
103 return ERR_PTR(-EINVAL);
104
Guenter Roeckbab22432013-07-06 13:57:23 -0700105 id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
106 if (id < 0)
107 return ERR_PTR(id);
108
109 hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
110 if (hwdev == NULL) {
111 err = -ENOMEM;
112 goto ida_remove;
113 }
114
115 hwdev->name = name;
116 hwdev->dev.class = &hwmon_class;
117 hwdev->dev.parent = dev;
118 hwdev->dev.groups = groups;
119 hwdev->dev.of_node = dev ? dev->of_node : NULL;
120 dev_set_drvdata(&hwdev->dev, drvdata);
121 dev_set_name(&hwdev->dev, HWMON_ID_FORMAT, id);
122 err = device_register(&hwdev->dev);
123 if (err)
124 goto free;
125
126 return &hwdev->dev;
127
128free:
129 kfree(hwdev);
130ida_remove:
131 ida_simple_remove(&hwmon_ida, id);
132 return ERR_PTR(err);
133}
134EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
135
136/**
Tony Jones1beeffe2007-08-20 13:46:20 -0700137 * hwmon_device_register - register w/ hwmon
Mark M. Hoffman12364412005-07-15 21:38:08 -0400138 * @dev: the device to register
139 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700140 * hwmon_device_unregister() must be called when the device is no
Mark M. Hoffman12364412005-07-15 21:38:08 -0400141 * longer needed.
142 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700143 * Returns the pointer to the new device.
Mark M. Hoffman12364412005-07-15 21:38:08 -0400144 */
Tony Jones1beeffe2007-08-20 13:46:20 -0700145struct device *hwmon_device_register(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -0400146{
Guenter Roeckbab22432013-07-06 13:57:23 -0700147 return hwmon_device_register_with_groups(dev, NULL, NULL, NULL);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400148}
Frans Meulenbroeks839a9ee2012-01-08 19:34:14 +0100149EXPORT_SYMBOL_GPL(hwmon_device_register);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400150
151/**
152 * hwmon_device_unregister - removes the previously registered class device
153 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700154 * @dev: the class device to destroy
Mark M. Hoffman12364412005-07-15 21:38:08 -0400155 */
Tony Jones1beeffe2007-08-20 13:46:20 -0700156void hwmon_device_unregister(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -0400157{
158 int id;
159
Kay Sievers739cf3a2009-01-06 10:44:41 -0800160 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
Tony Jones1beeffe2007-08-20 13:46:20 -0700161 device_unregister(dev);
Jonathan Cameron4ca5f462011-10-31 17:10:09 -0700162 ida_simple_remove(&hwmon_ida, id);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400163 } else
Tony Jones1beeffe2007-08-20 13:46:20 -0700164 dev_dbg(dev->parent,
Mark M. Hoffman12364412005-07-15 21:38:08 -0400165 "hwmon_device_unregister() failed: bad class ID!\n");
166}
Frans Meulenbroeks839a9ee2012-01-08 19:34:14 +0100167EXPORT_SYMBOL_GPL(hwmon_device_unregister);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400168
Guenter Roeck74188cb2013-07-11 20:00:12 -0700169static void devm_hwmon_release(struct device *dev, void *res)
170{
171 struct device *hwdev = *(struct device **)res;
172
173 hwmon_device_unregister(hwdev);
174}
175
176/**
177 * devm_hwmon_device_register_with_groups - register w/ hwmon
178 * @dev: the parent device
179 * @name: hwmon name attribute
180 * @drvdata: driver data to attach to created device
181 * @groups: List of attribute groups to create
182 *
183 * Returns the pointer to the new device. The new device is automatically
184 * unregistered with the parent device.
185 */
186struct device *
187devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
188 void *drvdata,
189 const struct attribute_group **groups)
190{
191 struct device **ptr, *hwdev;
192
193 if (!dev)
194 return ERR_PTR(-EINVAL);
195
196 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
197 if (!ptr)
198 return ERR_PTR(-ENOMEM);
199
200 hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
201 if (IS_ERR(hwdev))
202 goto error;
203
204 *ptr = hwdev;
205 devres_add(dev, ptr);
206 return hwdev;
207
208error:
209 devres_free(ptr);
210 return hwdev;
211}
212EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
213
214static int devm_hwmon_match(struct device *dev, void *res, void *data)
215{
216 struct device **hwdev = res;
217
218 return *hwdev == data;
219}
220
221/**
222 * devm_hwmon_device_unregister - removes a previously registered hwmon device
223 *
224 * @dev: the parent device of the device to unregister
225 */
226void devm_hwmon_device_unregister(struct device *dev)
227{
228 WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
229}
230EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
231
Jean Delvare2958b1e2009-06-15 18:39:50 +0200232static void __init hwmon_pci_quirks(void)
233{
234#if defined CONFIG_X86 && defined CONFIG_PCI
235 struct pci_dev *sb;
236 u16 base;
237 u8 enable;
238
239 /* Open access to 0x295-0x296 on MSI MS-7031 */
240 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
Jean Delvared6dab7d2012-12-19 22:16:59 +0100241 if (sb) {
242 if (sb->subsystem_vendor == 0x1462 && /* MSI */
243 sb->subsystem_device == 0x0031) { /* MS-7031 */
244 pci_read_config_byte(sb, 0x48, &enable);
245 pci_read_config_word(sb, 0x64, &base);
Jean Delvare2958b1e2009-06-15 18:39:50 +0200246
Jean Delvared6dab7d2012-12-19 22:16:59 +0100247 if (base == 0 && !(enable & BIT(2))) {
248 dev_info(&sb->dev,
249 "Opening wide generic port at 0x295\n");
250 pci_write_config_word(sb, 0x64, 0x295);
251 pci_write_config_byte(sb, 0x48,
252 enable | BIT(2));
253 }
Jean Delvare2958b1e2009-06-15 18:39:50 +0200254 }
Jean Delvared6dab7d2012-12-19 22:16:59 +0100255 pci_dev_put(sb);
Jean Delvare2958b1e2009-06-15 18:39:50 +0200256 }
257#endif
258}
259
Mark M. Hoffman12364412005-07-15 21:38:08 -0400260static int __init hwmon_init(void)
261{
Guenter Roeckbab22432013-07-06 13:57:23 -0700262 int err;
263
Jean Delvare2958b1e2009-06-15 18:39:50 +0200264 hwmon_pci_quirks();
265
Guenter Roeckbab22432013-07-06 13:57:23 -0700266 err = class_register(&hwmon_class);
267 if (err) {
268 pr_err("couldn't register hwmon sysfs class\n");
269 return err;
Mark M. Hoffman12364412005-07-15 21:38:08 -0400270 }
271 return 0;
272}
273
274static void __exit hwmon_exit(void)
275{
Guenter Roeckbab22432013-07-06 13:57:23 -0700276 class_unregister(&hwmon_class);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400277}
278
David Brownell37f54ee2007-02-14 21:15:04 +0100279subsys_initcall(hwmon_init);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400280module_exit(hwmon_exit);
281
Mark M. Hoffman12364412005-07-15 21:38:08 -0400282MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
283MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
284MODULE_LICENSE("GPL");
285