hwmon.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/err.h>
  16. #include <linux/kdev_t.h>
  17. #include <linux/idr.h>
  18. #include <linux/hwmon.h>
  19. #include <linux/gfp.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/pci.h>
  22. #define HWMON_ID_PREFIX "hwmon"
  23. #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
  24. static struct class *hwmon_class;
  25. static DEFINE_IDA(hwmon_ida);
  26. /**
  27. * hwmon_device_register - register w/ hwmon
  28. * @dev: the device to register
  29. *
  30. * hwmon_device_unregister() must be called when the device is no
  31. * longer needed.
  32. *
  33. * Returns the pointer to the new device.
  34. */
  35. struct device *hwmon_device_register(struct device *dev)
  36. {
  37. struct device *hwdev;
  38. int id;
  39. id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
  40. if (id < 0)
  41. return ERR_PTR(id);
  42. hwdev = device_create(hwmon_class, dev, MKDEV(0, 0), NULL,
  43. HWMON_ID_FORMAT, id);
  44. if (IS_ERR(hwdev))
  45. ida_simple_remove(&hwmon_ida, id);
  46. return hwdev;
  47. }
  48. EXPORT_SYMBOL_GPL(hwmon_device_register);
  49. /**
  50. * hwmon_device_unregister - removes the previously registered class device
  51. *
  52. * @dev: the class device to destroy
  53. */
  54. void hwmon_device_unregister(struct device *dev)
  55. {
  56. int id;
  57. if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
  58. device_unregister(dev);
  59. ida_simple_remove(&hwmon_ida, id);
  60. } else
  61. dev_dbg(dev->parent,
  62. "hwmon_device_unregister() failed: bad class ID!\n");
  63. }
  64. EXPORT_SYMBOL_GPL(hwmon_device_unregister);
  65. static void __init hwmon_pci_quirks(void)
  66. {
  67. #if defined CONFIG_X86 && defined CONFIG_PCI
  68. struct pci_dev *sb;
  69. u16 base;
  70. u8 enable;
  71. /* Open access to 0x295-0x296 on MSI MS-7031 */
  72. sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
  73. if (sb &&
  74. (sb->subsystem_vendor == 0x1462 && /* MSI */
  75. sb->subsystem_device == 0x0031)) { /* MS-7031 */
  76. pci_read_config_byte(sb, 0x48, &enable);
  77. pci_read_config_word(sb, 0x64, &base);
  78. if (base == 0 && !(enable & BIT(2))) {
  79. dev_info(&sb->dev,
  80. "Opening wide generic port at 0x295\n");
  81. pci_write_config_word(sb, 0x64, 0x295);
  82. pci_write_config_byte(sb, 0x48, enable | BIT(2));
  83. }
  84. }
  85. #endif
  86. }
  87. static int __init hwmon_init(void)
  88. {
  89. hwmon_pci_quirks();
  90. hwmon_class = class_create(THIS_MODULE, "hwmon");
  91. if (IS_ERR(hwmon_class)) {
  92. pr_err("couldn't create sysfs class\n");
  93. return PTR_ERR(hwmon_class);
  94. }
  95. return 0;
  96. }
  97. static void __exit hwmon_exit(void)
  98. {
  99. class_destroy(hwmon_class);
  100. }
  101. subsys_initcall(hwmon_init);
  102. module_exit(hwmon_exit);
  103. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  104. MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
  105. MODULE_LICENSE("GPL");