i5500_temp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/bitops.h>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/device.h>
  11. #include <linux/pci.h>
  12. #include <linux/hwmon.h>
  13. #include <linux/err.h>
  14. /* Register definitions from datasheet */
  15. #define REG_TSTHRCATA 0xE2
  16. #define REG_TSCTRL 0xE8
  17. #define REG_TSTHRRPEX 0xEB
  18. #define REG_TSTHRLO 0xEC
  19. #define REG_TSTHRHI 0xEE
  20. #define REG_CTHINT 0xF0
  21. #define REG_TSFSC 0xF3
  22. #define REG_CTSTS 0xF4
  23. #define REG_TSTHRRQPI 0xF5
  24. #define REG_CTCTRL 0xF7
  25. #define REG_TSTIMER 0xF8
  26. static int i5500_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
  27. long *val)
  28. {
  29. struct pci_dev *pdev = to_pci_dev(dev->parent);
  30. u16 tsthr;
  31. s8 tsfsc;
  32. u8 ctsts;
  33. switch (type) {
  34. case hwmon_temp:
  35. switch (attr) {
  36. /* Sensor resolution : 0.5 degree C */
  37. case hwmon_temp_input:
  38. pci_read_config_word(pdev, REG_TSTHRHI, &tsthr);
  39. pci_read_config_byte(pdev, REG_TSFSC, &tsfsc);
  40. *val = (tsthr - tsfsc) * 500;
  41. return 0;
  42. case hwmon_temp_max:
  43. pci_read_config_word(pdev, REG_TSTHRHI, &tsthr);
  44. *val = tsthr * 500;
  45. return 0;
  46. case hwmon_temp_max_hyst:
  47. pci_read_config_word(pdev, REG_TSTHRLO, &tsthr);
  48. *val = tsthr * 500;
  49. return 0;
  50. case hwmon_temp_crit:
  51. pci_read_config_word(pdev, REG_TSTHRCATA, &tsthr);
  52. *val = tsthr * 500;
  53. return 0;
  54. case hwmon_temp_max_alarm:
  55. pci_read_config_byte(pdev, REG_CTSTS, &ctsts);
  56. *val = !!(ctsts & BIT(1));
  57. return 0;
  58. case hwmon_temp_crit_alarm:
  59. pci_read_config_byte(pdev, REG_CTSTS, &ctsts);
  60. *val = !!(ctsts & BIT(0));
  61. return 0;
  62. default:
  63. break;
  64. }
  65. break;
  66. default:
  67. break;
  68. }
  69. return -EOPNOTSUPP;
  70. }
  71. static const struct hwmon_ops i5500_ops = {
  72. .visible = 0444,
  73. .read = i5500_read,
  74. };
  75. static const struct hwmon_channel_info * const i5500_info[] = {
  76. HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
  77. HWMON_CHANNEL_INFO(temp,
  78. HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST | HWMON_T_CRIT |
  79. HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM
  80. ),
  81. NULL
  82. };
  83. static const struct hwmon_chip_info i5500_chip_info = {
  84. .ops = &i5500_ops,
  85. .info = i5500_info,
  86. };
  87. static const struct pci_device_id i5500_temp_ids[] = {
  88. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x3438) },
  89. { 0 },
  90. };
  91. MODULE_DEVICE_TABLE(pci, i5500_temp_ids);
  92. static int i5500_temp_probe(struct pci_dev *pdev,
  93. const struct pci_device_id *id)
  94. {
  95. int err;
  96. struct device *hwmon_dev;
  97. u32 tstimer;
  98. s8 tsfsc;
  99. err = pcim_enable_device(pdev);
  100. if (err) {
  101. dev_err(&pdev->dev, "Failed to enable device\n");
  102. return err;
  103. }
  104. pci_read_config_byte(pdev, REG_TSFSC, &tsfsc);
  105. pci_read_config_dword(pdev, REG_TSTIMER, &tstimer);
  106. if (tsfsc == 0x7F && tstimer == 0x07D30D40) {
  107. dev_notice(&pdev->dev, "Sensor seems to be disabled\n");
  108. return -ENODEV;
  109. }
  110. hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev, "intel5500", NULL,
  111. &i5500_chip_info, NULL);
  112. return PTR_ERR_OR_ZERO(hwmon_dev);
  113. }
  114. static struct pci_driver i5500_temp_driver = {
  115. .name = "i5500_temp",
  116. .id_table = i5500_temp_ids,
  117. .probe = i5500_temp_probe,
  118. };
  119. module_pci_driver(i5500_temp_driver);
  120. MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
  121. MODULE_DESCRIPTION("Intel 5500/5520/X58 chipset thermal sensor driver");
  122. MODULE_LICENSE("GPL");