Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 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 | |
Joe Perches | c95df1a | 2010-10-20 06:51:37 +0000 | [diff] [blame] | 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 | |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 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> |
Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 21 | #include <linux/gfp.h> |
Mark M. Hoffman | ded2b66 | 2006-03-05 23:13:47 +0100 | [diff] [blame] | 22 | #include <linux/spinlock.h> |
Jean Delvare | 2958b1e | 2009-06-15 18:39:50 +0200 | [diff] [blame] | 23 | #include <linux/pci.h> |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 24 | |
| 25 | #define HWMON_ID_PREFIX "hwmon" |
| 26 | #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d" |
| 27 | |
| 28 | static struct class *hwmon_class; |
| 29 | |
Jonathan Cameron | 4ca5f46 | 2011-10-31 17:10:09 -0700 | [diff] [blame] | 30 | static DEFINE_IDA(hwmon_ida); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 31 | |
| 32 | /** |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 33 | * hwmon_device_register - register w/ hwmon |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 34 | * @dev: the device to register |
| 35 | * |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 36 | * hwmon_device_unregister() must be called when the device is no |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 37 | * longer needed. |
| 38 | * |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 39 | * Returns the pointer to the new device. |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 40 | */ |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 41 | struct device *hwmon_device_register(struct device *dev) |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 42 | { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 43 | struct device *hwdev; |
Jonathan Cameron | 4ca5f46 | 2011-10-31 17:10:09 -0700 | [diff] [blame] | 44 | int id; |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 45 | |
Jonathan Cameron | 4ca5f46 | 2011-10-31 17:10:09 -0700 | [diff] [blame] | 46 | id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL); |
| 47 | if (id < 0) |
| 48 | return ERR_PTR(id); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 49 | |
Greg Kroah-Hartman | a9b1261 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 50 | hwdev = device_create(hwmon_class, dev, MKDEV(0, 0), NULL, |
| 51 | HWMON_ID_FORMAT, id); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 52 | |
Jonathan Cameron | 4ca5f46 | 2011-10-31 17:10:09 -0700 | [diff] [blame] | 53 | if (IS_ERR(hwdev)) |
| 54 | ida_simple_remove(&hwmon_ida, id); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 55 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 56 | return hwdev; |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
| 60 | * hwmon_device_unregister - removes the previously registered class device |
| 61 | * |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 62 | * @dev: the class device to destroy |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 63 | */ |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 64 | void hwmon_device_unregister(struct device *dev) |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 65 | { |
| 66 | int id; |
| 67 | |
Kay Sievers | 739cf3a | 2009-01-06 10:44:41 -0800 | [diff] [blame] | 68 | if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 69 | device_unregister(dev); |
Jonathan Cameron | 4ca5f46 | 2011-10-31 17:10:09 -0700 | [diff] [blame] | 70 | ida_simple_remove(&hwmon_ida, id); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 71 | } else |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 72 | dev_dbg(dev->parent, |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 73 | "hwmon_device_unregister() failed: bad class ID!\n"); |
| 74 | } |
| 75 | |
Jean Delvare | 2958b1e | 2009-06-15 18:39:50 +0200 | [diff] [blame] | 76 | static void __init hwmon_pci_quirks(void) |
| 77 | { |
| 78 | #if defined CONFIG_X86 && defined CONFIG_PCI |
| 79 | struct pci_dev *sb; |
| 80 | u16 base; |
| 81 | u8 enable; |
| 82 | |
| 83 | /* Open access to 0x295-0x296 on MSI MS-7031 */ |
| 84 | sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL); |
| 85 | if (sb && |
| 86 | (sb->subsystem_vendor == 0x1462 && /* MSI */ |
| 87 | sb->subsystem_device == 0x0031)) { /* MS-7031 */ |
| 88 | |
| 89 | pci_read_config_byte(sb, 0x48, &enable); |
| 90 | pci_read_config_word(sb, 0x64, &base); |
| 91 | |
| 92 | if (base == 0 && !(enable & BIT(2))) { |
| 93 | dev_info(&sb->dev, |
| 94 | "Opening wide generic port at 0x295\n"); |
| 95 | pci_write_config_word(sb, 0x64, 0x295); |
| 96 | pci_write_config_byte(sb, 0x48, enable | BIT(2)); |
| 97 | } |
| 98 | } |
| 99 | #endif |
| 100 | } |
| 101 | |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 102 | static int __init hwmon_init(void) |
| 103 | { |
Jean Delvare | 2958b1e | 2009-06-15 18:39:50 +0200 | [diff] [blame] | 104 | hwmon_pci_quirks(); |
| 105 | |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 106 | hwmon_class = class_create(THIS_MODULE, "hwmon"); |
| 107 | if (IS_ERR(hwmon_class)) { |
Joe Perches | c95df1a | 2010-10-20 06:51:37 +0000 | [diff] [blame] | 108 | pr_err("couldn't create sysfs class\n"); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 109 | return PTR_ERR(hwmon_class); |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static void __exit hwmon_exit(void) |
| 115 | { |
| 116 | class_destroy(hwmon_class); |
| 117 | } |
| 118 | |
David Brownell | 37f54ee | 2007-02-14 21:15:04 +0100 | [diff] [blame] | 119 | subsys_initcall(hwmon_init); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 120 | module_exit(hwmon_exit); |
| 121 | |
| 122 | EXPORT_SYMBOL_GPL(hwmon_device_register); |
| 123 | EXPORT_SYMBOL_GPL(hwmon_device_unregister); |
| 124 | |
| 125 | MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); |
| 126 | MODULE_DESCRIPTION("hardware monitoring sysfs/class support"); |
| 127 | MODULE_LICENSE("GPL"); |
| 128 | |