i5500_temp.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * i5500_temp - Driver for Intel 5500/5520/X58 chipset thermal sensor
  3. *
  4. * Copyright (C) 2012, 2014 Jean Delvare <jdelvare@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/device.h>
  21. #include <linux/pci.h>
  22. #include <linux/hwmon.h>
  23. #include <linux/hwmon-sysfs.h>
  24. #include <linux/err.h>
  25. #include <linux/mutex.h>
  26. /* Register definitions from datasheet */
  27. #define REG_TSTHRCATA 0xE2
  28. #define REG_TSCTRL 0xE8
  29. #define REG_TSTHRRPEX 0xEB
  30. #define REG_TSTHRLO 0xEC
  31. #define REG_TSTHRHI 0xEE
  32. #define REG_CTHINT 0xF0
  33. #define REG_TSFSC 0xF3
  34. #define REG_CTSTS 0xF4
  35. #define REG_TSTHRRQPI 0xF5
  36. #define REG_CTCTRL 0xF7
  37. #define REG_TSTIMER 0xF8
  38. /*
  39. * Sysfs stuff
  40. */
  41. /* Sensor resolution : 0.5 degree C */
  42. static ssize_t show_temp(struct device *dev,
  43. struct device_attribute *devattr, char *buf)
  44. {
  45. struct pci_dev *pdev = to_pci_dev(dev->parent);
  46. long temp;
  47. u16 tsthrhi;
  48. s8 tsfsc;
  49. pci_read_config_word(pdev, REG_TSTHRHI, &tsthrhi);
  50. pci_read_config_byte(pdev, REG_TSFSC, &tsfsc);
  51. temp = ((long)tsthrhi - tsfsc) * 500;
  52. return sprintf(buf, "%ld\n", temp);
  53. }
  54. static ssize_t show_thresh(struct device *dev,
  55. struct device_attribute *devattr, char *buf)
  56. {
  57. struct pci_dev *pdev = to_pci_dev(dev->parent);
  58. int reg = to_sensor_dev_attr(devattr)->index;
  59. long temp;
  60. u16 tsthr;
  61. pci_read_config_word(pdev, reg, &tsthr);
  62. temp = tsthr * 500;
  63. return sprintf(buf, "%ld\n", temp);
  64. }
  65. static ssize_t show_alarm(struct device *dev,
  66. struct device_attribute *devattr, char *buf)
  67. {
  68. struct pci_dev *pdev = to_pci_dev(dev->parent);
  69. int nr = to_sensor_dev_attr(devattr)->index;
  70. u8 ctsts;
  71. pci_read_config_byte(pdev, REG_CTSTS, &ctsts);
  72. return sprintf(buf, "%u\n", (unsigned int)ctsts & (1 << nr));
  73. }
  74. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
  75. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_thresh, NULL, 0xE2);
  76. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO, show_thresh, NULL, 0xEC);
  77. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_thresh, NULL, 0xEE);
  78. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
  79. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 1);
  80. static struct attribute *i5500_temp_attrs[] = {
  81. &dev_attr_temp1_input.attr,
  82. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  83. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  84. &sensor_dev_attr_temp1_max.dev_attr.attr,
  85. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  86. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  87. NULL
  88. };
  89. ATTRIBUTE_GROUPS(i5500_temp);
  90. static const struct pci_device_id i5500_temp_ids[] = {
  91. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x3438) },
  92. { 0 },
  93. };
  94. MODULE_DEVICE_TABLE(pci, i5500_temp_ids);
  95. static int i5500_temp_probe(struct pci_dev *pdev,
  96. const struct pci_device_id *id)
  97. {
  98. int err;
  99. struct device *hwmon_dev;
  100. u32 tstimer;
  101. s8 tsfsc;
  102. err = pci_enable_device(pdev);
  103. if (err) {
  104. dev_err(&pdev->dev, "Failed to enable device\n");
  105. return err;
  106. }
  107. pci_read_config_byte(pdev, REG_TSFSC, &tsfsc);
  108. pci_read_config_dword(pdev, REG_TSTIMER, &tstimer);
  109. if (tsfsc == 0x7F && tstimer == 0x07D30D40) {
  110. dev_notice(&pdev->dev, "Sensor seems to be disabled\n");
  111. return -ENODEV;
  112. }
  113. hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev,
  114. "intel5500", NULL,
  115. i5500_temp_groups);
  116. return PTR_ERR_OR_ZERO(hwmon_dev);
  117. }
  118. static struct pci_driver i5500_temp_driver = {
  119. .name = "i5500_temp",
  120. .id_table = i5500_temp_ids,
  121. .probe = i5500_temp_probe,
  122. };
  123. module_pci_driver(i5500_temp_driver);
  124. MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
  125. MODULE_DESCRIPTION("Intel 5500/5520/X58 chipset thermal sensor driver");
  126. MODULE_LICENSE("GPL");