Commit | Line | Data |
---|---|---|
1236441f | 1 | /* |
5ed04880 GR |
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 | */ | |
1236441f | 12 | |
c95df1ae JP |
13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
14 | ||
1236441f MH |
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> | |
8c65b4a6 | 21 | #include <linux/gfp.h> |
ded2b666 | 22 | #include <linux/spinlock.h> |
2958b1ec | 23 | #include <linux/pci.h> |
1236441f MH |
24 | |
25 | #define HWMON_ID_PREFIX "hwmon" | |
26 | #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d" | |
27 | ||
28 | static struct class *hwmon_class; | |
29 | ||
4ca5f468 | 30 | static DEFINE_IDA(hwmon_ida); |
1236441f MH |
31 | |
32 | /** | |
1beeffe4 | 33 | * hwmon_device_register - register w/ hwmon |
1236441f MH |
34 | * @dev: the device to register |
35 | * | |
1beeffe4 | 36 | * hwmon_device_unregister() must be called when the device is no |
1236441f MH |
37 | * longer needed. |
38 | * | |
1beeffe4 | 39 | * Returns the pointer to the new device. |
1236441f | 40 | */ |
1beeffe4 | 41 | struct device *hwmon_device_register(struct device *dev) |
1236441f | 42 | { |
1beeffe4 | 43 | struct device *hwdev; |
4ca5f468 | 44 | int id; |
ded2b666 | 45 | |
4ca5f468 JC |
46 | id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL); |
47 | if (id < 0) | |
48 | return ERR_PTR(id); | |
1236441f | 49 | |
a9b12619 GKH |
50 | hwdev = device_create(hwmon_class, dev, MKDEV(0, 0), NULL, |
51 | HWMON_ID_FORMAT, id); | |
1236441f | 52 | |
4ca5f468 JC |
53 | if (IS_ERR(hwdev)) |
54 | ida_simple_remove(&hwmon_ida, id); | |
1236441f | 55 | |
1beeffe4 | 56 | return hwdev; |
1236441f | 57 | } |
839a9eef | 58 | EXPORT_SYMBOL_GPL(hwmon_device_register); |
1236441f MH |
59 | |
60 | /** | |
61 | * hwmon_device_unregister - removes the previously registered class device | |
62 | * | |
1beeffe4 | 63 | * @dev: the class device to destroy |
1236441f | 64 | */ |
1beeffe4 | 65 | void hwmon_device_unregister(struct device *dev) |
1236441f MH |
66 | { |
67 | int id; | |
68 | ||
739cf3a2 | 69 | if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) { |
1beeffe4 | 70 | device_unregister(dev); |
4ca5f468 | 71 | ida_simple_remove(&hwmon_ida, id); |
1236441f | 72 | } else |
1beeffe4 | 73 | dev_dbg(dev->parent, |
1236441f MH |
74 | "hwmon_device_unregister() failed: bad class ID!\n"); |
75 | } | |
839a9eef | 76 | EXPORT_SYMBOL_GPL(hwmon_device_unregister); |
1236441f | 77 | |
2958b1ec JD |
78 | static void __init hwmon_pci_quirks(void) |
79 | { | |
80 | #if defined CONFIG_X86 && defined CONFIG_PCI | |
81 | struct pci_dev *sb; | |
82 | u16 base; | |
83 | u8 enable; | |
84 | ||
85 | /* Open access to 0x295-0x296 on MSI MS-7031 */ | |
86 | sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL); | |
d6dab7dd JD |
87 | if (sb) { |
88 | if (sb->subsystem_vendor == 0x1462 && /* MSI */ | |
89 | sb->subsystem_device == 0x0031) { /* MS-7031 */ | |
90 | pci_read_config_byte(sb, 0x48, &enable); | |
91 | pci_read_config_word(sb, 0x64, &base); | |
92 | ||
93 | if (base == 0 && !(enable & BIT(2))) { | |
94 | dev_info(&sb->dev, | |
95 | "Opening wide generic port at 0x295\n"); | |
96 | pci_write_config_word(sb, 0x64, 0x295); | |
97 | pci_write_config_byte(sb, 0x48, | |
98 | enable | BIT(2)); | |
99 | } | |
2958b1ec | 100 | } |
d6dab7dd | 101 | pci_dev_put(sb); |
2958b1ec JD |
102 | } |
103 | #endif | |
104 | } | |
105 | ||
1236441f MH |
106 | static int __init hwmon_init(void) |
107 | { | |
2958b1ec JD |
108 | hwmon_pci_quirks(); |
109 | ||
1236441f MH |
110 | hwmon_class = class_create(THIS_MODULE, "hwmon"); |
111 | if (IS_ERR(hwmon_class)) { | |
c95df1ae | 112 | pr_err("couldn't create sysfs class\n"); |
1236441f MH |
113 | return PTR_ERR(hwmon_class); |
114 | } | |
115 | return 0; | |
116 | } | |
117 | ||
118 | static void __exit hwmon_exit(void) | |
119 | { | |
120 | class_destroy(hwmon_class); | |
121 | } | |
122 | ||
37f54ee5 | 123 | subsys_initcall(hwmon_init); |
1236441f MH |
124 | module_exit(hwmon_exit); |
125 | ||
1236441f MH |
126 | MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); |
127 | MODULE_DESCRIPTION("hardware monitoring sysfs/class support"); | |
128 | MODULE_LICENSE("GPL"); | |
129 |