fan_hwmon.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * hwmon interface for the ACPI Fan driver.
  4. *
  5. * Copyright (C) 2024 Armin Wolf <W_Armin@gmx.de>
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/device.h>
  9. #include <linux/err.h>
  10. #include <linux/hwmon.h>
  11. #include <linux/limits.h>
  12. #include <linux/types.h>
  13. #include <linux/units.h>
  14. #include "fan.h"
  15. /* Returned when the ACPI fan does not support speed reporting */
  16. #define FAN_SPEED_UNAVAILABLE U32_MAX
  17. #define FAN_POWER_UNAVAILABLE U32_MAX
  18. static struct acpi_fan_fps *acpi_fan_get_current_fps(struct acpi_fan *fan, u64 control)
  19. {
  20. unsigned int i;
  21. for (i = 0; i < fan->fps_count; i++) {
  22. if (fan->fps[i].control == control)
  23. return &fan->fps[i];
  24. }
  25. return NULL;
  26. }
  27. static umode_t acpi_fan_hwmon_is_visible(const void *drvdata, enum hwmon_sensor_types type,
  28. u32 attr, int channel)
  29. {
  30. const struct acpi_fan *fan = drvdata;
  31. unsigned int i;
  32. switch (type) {
  33. case hwmon_fan:
  34. switch (attr) {
  35. case hwmon_fan_input:
  36. return 0444;
  37. case hwmon_fan_target:
  38. /*
  39. * When in fine grain control mode, not every fan control value
  40. * has an associated fan performance state.
  41. */
  42. if (fan->fif.fine_grain_ctrl)
  43. return 0;
  44. return 0444;
  45. default:
  46. return 0;
  47. }
  48. case hwmon_power:
  49. switch (attr) {
  50. case hwmon_power_input:
  51. /*
  52. * When in fine grain control mode, not every fan control value
  53. * has an associated fan performance state.
  54. */
  55. if (fan->fif.fine_grain_ctrl)
  56. return 0;
  57. /*
  58. * When all fan performance states contain no valid power data,
  59. * when the associated attribute should not be created.
  60. */
  61. for (i = 0; i < fan->fps_count; i++) {
  62. if (fan->fps[i].power != FAN_POWER_UNAVAILABLE)
  63. return 0444;
  64. }
  65. return 0;
  66. default:
  67. return 0;
  68. }
  69. default:
  70. return 0;
  71. }
  72. }
  73. static int acpi_fan_hwmon_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
  74. int channel, long *val)
  75. {
  76. struct acpi_device *adev = to_acpi_device(dev->parent);
  77. struct acpi_fan *fan = dev_get_drvdata(dev);
  78. struct acpi_fan_fps *fps;
  79. struct acpi_fan_fst fst;
  80. int ret;
  81. ret = acpi_fan_get_fst(adev, &fst);
  82. if (ret < 0)
  83. return ret;
  84. switch (type) {
  85. case hwmon_fan:
  86. switch (attr) {
  87. case hwmon_fan_input:
  88. if (fst.speed == FAN_SPEED_UNAVAILABLE)
  89. return -ENODEV;
  90. if (fst.speed > LONG_MAX)
  91. return -EOVERFLOW;
  92. *val = fst.speed;
  93. return 0;
  94. case hwmon_fan_target:
  95. fps = acpi_fan_get_current_fps(fan, fst.control);
  96. if (!fps)
  97. return -EIO;
  98. if (fps->speed > LONG_MAX)
  99. return -EOVERFLOW;
  100. *val = fps->speed;
  101. return 0;
  102. default:
  103. return -EOPNOTSUPP;
  104. }
  105. case hwmon_power:
  106. switch (attr) {
  107. case hwmon_power_input:
  108. fps = acpi_fan_get_current_fps(fan, fst.control);
  109. if (!fps)
  110. return -EIO;
  111. if (fps->power == FAN_POWER_UNAVAILABLE)
  112. return -ENODEV;
  113. if (fps->power > LONG_MAX / MICROWATT_PER_MILLIWATT)
  114. return -EOVERFLOW;
  115. *val = fps->power * MICROWATT_PER_MILLIWATT;
  116. return 0;
  117. default:
  118. return -EOPNOTSUPP;
  119. }
  120. default:
  121. return -EOPNOTSUPP;
  122. }
  123. }
  124. static const struct hwmon_ops acpi_fan_hwmon_ops = {
  125. .is_visible = acpi_fan_hwmon_is_visible,
  126. .read = acpi_fan_hwmon_read,
  127. };
  128. static const struct hwmon_channel_info * const acpi_fan_hwmon_info[] = {
  129. HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_TARGET),
  130. HWMON_CHANNEL_INFO(power, HWMON_P_INPUT),
  131. NULL
  132. };
  133. static const struct hwmon_chip_info acpi_fan_hwmon_chip_info = {
  134. .ops = &acpi_fan_hwmon_ops,
  135. .info = acpi_fan_hwmon_info,
  136. };
  137. int devm_acpi_fan_create_hwmon(struct acpi_device *device)
  138. {
  139. struct acpi_fan *fan = acpi_driver_data(device);
  140. struct device *hdev;
  141. hdev = devm_hwmon_device_register_with_info(&device->dev, "acpi_fan", fan,
  142. &acpi_fan_hwmon_chip_info, NULL);
  143. return PTR_ERR_OR_ZERO(hdev);
  144. }