i5500_temp.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * i5500_temp - Driver for Intel 5500/5520/X58 chipset thermal sensor
  4. *
  5. * Copyright (C) 2012, 2014 Jean Delvare <jdelvare@suse.de>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/jiffies.h>
  11. #include <linux/device.h>
  12. #include <linux/pci.h>
  13. #include <linux/hwmon.h>
  14. #include <linux/hwmon-sysfs.h>
  15. #include <linux/err.h>
  16. #include <linux/mutex.h>
  17. /* Register definitions from datasheet */
  18. #define REG_TSTHRCATA 0xE2
  19. #define REG_TSCTRL 0xE8
  20. #define REG_TSTHRRPEX 0xEB
  21. #define REG_TSTHRLO 0xEC
  22. #define REG_TSTHRHI 0xEE
  23. #define REG_CTHINT 0xF0
  24. #define REG_TSFSC 0xF3
  25. #define REG_CTSTS 0xF4
  26. #define REG_TSTHRRQPI 0xF5
  27. #define REG_CTCTRL 0xF7
  28. #define REG_TSTIMER 0xF8
  29. /*
  30. * Sysfs stuff
  31. */
  32. /* Sensor resolution : 0.5 degree C */
  33. static ssize_t temp1_input_show(struct device *dev,
  34. struct device_attribute *devattr, char *buf)
  35. {
  36. struct pci_dev *pdev = to_pci_dev(dev->parent);
  37. long temp;
  38. u16 tsthrhi;
  39. s8 tsfsc;
  40. pci_read_config_word(pdev, REG_TSTHRHI, &tsthrhi);
  41. pci_read_config_byte(pdev, REG_TSFSC, &tsfsc);
  42. temp = ((long)tsthrhi - tsfsc) * 500;
  43. return sprintf(buf, "%ld\n", temp);
  44. }
  45. static ssize_t thresh_show(struct device *dev,
  46. struct device_attribute *devattr, char *buf)
  47. {
  48. struct pci_dev *pdev = to_pci_dev(dev->parent);
  49. int reg = to_sensor_dev_attr(devattr)->index;
  50. long temp;
  51. u16 tsthr;
  52. pci_read_config_word(pdev, reg, &tsthr);
  53. temp = tsthr * 500;
  54. return sprintf(buf, "%ld\n", temp);
  55. }
  56. static ssize_t alarm_show(struct device *dev,
  57. struct device_attribute *devattr, char *buf)
  58. {
  59. struct pci_dev *pdev = to_pci_dev(dev->parent);
  60. int nr = to_sensor_dev_attr(devattr)->index;
  61. u8 ctsts;
  62. pci_read_config_byte(pdev, REG_CTSTS, &ctsts);
  63. return sprintf(buf, "%u\n", (unsigned int)ctsts & (1 << nr));
  64. }
  65. static DEVICE_ATTR_RO(temp1_input);
  66. static SENSOR_DEVICE_ATTR_RO(temp1_crit, thresh, 0xE2);
  67. static SENSOR_DEVICE_ATTR_RO(temp1_max_hyst, thresh, 0xEC);
  68. static SENSOR_DEVICE_ATTR_RO(temp1_max, thresh, 0xEE);
  69. static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 0);
  70. static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 1);
  71. static struct attribute *i5500_temp_attrs[] = {
  72. &dev_attr_temp1_input.attr,
  73. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  74. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  75. &sensor_dev_attr_temp1_max.dev_attr.attr,
  76. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  77. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  78. NULL
  79. };
  80. ATTRIBUTE_GROUPS(i5500_temp);
  81. static const struct pci_device_id i5500_temp_ids[] = {
  82. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x3438) },
  83. { 0 },
  84. };
  85. MODULE_DEVICE_TABLE(pci, i5500_temp_ids);
  86. static int i5500_temp_probe(struct pci_dev *pdev,
  87. const struct pci_device_id *id)
  88. {
  89. int err;
  90. struct device *hwmon_dev;
  91. u32 tstimer;
  92. s8 tsfsc;
  93. err = pci_enable_device(pdev);
  94. if (err) {
  95. dev_err(&pdev->dev, "Failed to enable device\n");
  96. return err;
  97. }
  98. pci_read_config_byte(pdev, REG_TSFSC, &tsfsc);
  99. pci_read_config_dword(pdev, REG_TSTIMER, &tstimer);
  100. if (tsfsc == 0x7F && tstimer == 0x07D30D40) {
  101. dev_notice(&pdev->dev, "Sensor seems to be disabled\n");
  102. return -ENODEV;
  103. }
  104. hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev,
  105. "intel5500", NULL,
  106. i5500_temp_groups);
  107. return PTR_ERR_OR_ZERO(hwmon_dev);
  108. }
  109. static struct pci_driver i5500_temp_driver = {
  110. .name = "i5500_temp",
  111. .id_table = i5500_temp_ids,
  112. .probe = i5500_temp_probe,
  113. };
  114. module_pci_driver(i5500_temp_driver);
  115. MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
  116. MODULE_DESCRIPTION("Intel 5500/5520/X58 chipset thermal sensor driver");
  117. MODULE_LICENSE("GPL");