blob: 646314f7c8397e05cfb1c89da7786005e6e41b7a [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/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>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080021#include <linux/gfp.h>
Mark M. Hoffmanded2b662006-03-05 23:13:47 +010022#include <linux/spinlock.h>
Jean Delvare2958b1e2009-06-15 18:39:50 +020023#include <linux/pci.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
28static struct class *hwmon_class;
29
Jonathan Cameron4ca5f462011-10-31 17:10:09 -070030static DEFINE_IDA(hwmon_ida);
Mark M. Hoffman12364412005-07-15 21:38:08 -040031
32/**
Tony Jones1beeffe2007-08-20 13:46:20 -070033 * hwmon_device_register - register w/ hwmon
Mark M. Hoffman12364412005-07-15 21:38:08 -040034 * @dev: the device to register
35 *
Tony Jones1beeffe2007-08-20 13:46:20 -070036 * hwmon_device_unregister() must be called when the device is no
Mark M. Hoffman12364412005-07-15 21:38:08 -040037 * longer needed.
38 *
Tony Jones1beeffe2007-08-20 13:46:20 -070039 * Returns the pointer to the new device.
Mark M. Hoffman12364412005-07-15 21:38:08 -040040 */
Tony Jones1beeffe2007-08-20 13:46:20 -070041struct device *hwmon_device_register(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -040042{
Tony Jones1beeffe2007-08-20 13:46:20 -070043 struct device *hwdev;
Jonathan Cameron4ca5f462011-10-31 17:10:09 -070044 int id;
Mark M. Hoffman12364412005-07-15 21:38:08 -040045
Jonathan Cameron4ca5f462011-10-31 17:10:09 -070046 id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
47 if (id < 0)
48 return ERR_PTR(id);
Mark M. Hoffman12364412005-07-15 21:38:08 -040049
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -070050 hwdev = device_create(hwmon_class, dev, MKDEV(0, 0), NULL,
51 HWMON_ID_FORMAT, id);
Mark M. Hoffman12364412005-07-15 21:38:08 -040052
Jonathan Cameron4ca5f462011-10-31 17:10:09 -070053 if (IS_ERR(hwdev))
54 ida_simple_remove(&hwmon_ida, id);
Mark M. Hoffman12364412005-07-15 21:38:08 -040055
Tony Jones1beeffe2007-08-20 13:46:20 -070056 return hwdev;
Mark M. Hoffman12364412005-07-15 21:38:08 -040057}
Frans Meulenbroeks839a9ee2012-01-08 19:34:14 +010058EXPORT_SYMBOL_GPL(hwmon_device_register);
Mark M. Hoffman12364412005-07-15 21:38:08 -040059
60/**
61 * hwmon_device_unregister - removes the previously registered class device
62 *
Tony Jones1beeffe2007-08-20 13:46:20 -070063 * @dev: the class device to destroy
Mark M. Hoffman12364412005-07-15 21:38:08 -040064 */
Tony Jones1beeffe2007-08-20 13:46:20 -070065void hwmon_device_unregister(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -040066{
67 int id;
68
Kay Sievers739cf3a2009-01-06 10:44:41 -080069 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
Tony Jones1beeffe2007-08-20 13:46:20 -070070 device_unregister(dev);
Jonathan Cameron4ca5f462011-10-31 17:10:09 -070071 ida_simple_remove(&hwmon_ida, id);
Mark M. Hoffman12364412005-07-15 21:38:08 -040072 } else
Tony Jones1beeffe2007-08-20 13:46:20 -070073 dev_dbg(dev->parent,
Mark M. Hoffman12364412005-07-15 21:38:08 -040074 "hwmon_device_unregister() failed: bad class ID!\n");
75}
Frans Meulenbroeks839a9ee2012-01-08 19:34:14 +010076EXPORT_SYMBOL_GPL(hwmon_device_unregister);
Mark M. Hoffman12364412005-07-15 21:38:08 -040077
Jean Delvare2958b1e2009-06-15 18:39:50 +020078static 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);
Jean Delvared6dab7d2012-12-19 22:16:59 +010087 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);
Jean Delvare2958b1e2009-06-15 18:39:50 +020092
Jean Delvared6dab7d2012-12-19 22:16:59 +010093 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 }
Jean Delvare2958b1e2009-06-15 18:39:50 +0200100 }
Jean Delvared6dab7d2012-12-19 22:16:59 +0100101 pci_dev_put(sb);
Jean Delvare2958b1e2009-06-15 18:39:50 +0200102 }
103#endif
104}
105
Mark M. Hoffman12364412005-07-15 21:38:08 -0400106static int __init hwmon_init(void)
107{
Jean Delvare2958b1e2009-06-15 18:39:50 +0200108 hwmon_pci_quirks();
109
Mark M. Hoffman12364412005-07-15 21:38:08 -0400110 hwmon_class = class_create(THIS_MODULE, "hwmon");
111 if (IS_ERR(hwmon_class)) {
Joe Perchesc95df1a2010-10-20 06:51:37 +0000112 pr_err("couldn't create sysfs class\n");
Mark M. Hoffman12364412005-07-15 21:38:08 -0400113 return PTR_ERR(hwmon_class);
114 }
115 return 0;
116}
117
118static void __exit hwmon_exit(void)
119{
120 class_destroy(hwmon_class);
121}
122
David Brownell37f54ee2007-02-14 21:15:04 +0100123subsys_initcall(hwmon_init);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400124module_exit(hwmon_exit);
125
Mark M. Hoffman12364412005-07-15 21:38:08 -0400126MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
127MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
128MODULE_LICENSE("GPL");
129