fan_attr.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * fan_attr.c - Create extra attributes for ACPI Fan driver
  4. *
  5. * Copyright (C) 2001, 2002 Andy Grover <[email protected]>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <[email protected]>
  7. * Copyright (C) 2022 Intel Corporation. All rights reserved.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/acpi.h>
  13. #include "fan.h"
  14. MODULE_LICENSE("GPL");
  15. static ssize_t show_state(struct device *dev, struct device_attribute *attr, char *buf)
  16. {
  17. struct acpi_fan_fps *fps = container_of(attr, struct acpi_fan_fps, dev_attr);
  18. int count;
  19. if (fps->control == 0xFFFFFFFF || fps->control > 100)
  20. count = scnprintf(buf, PAGE_SIZE, "not-defined:");
  21. else
  22. count = scnprintf(buf, PAGE_SIZE, "%lld:", fps->control);
  23. if (fps->trip_point == 0xFFFFFFFF || fps->trip_point > 9)
  24. count += scnprintf(&buf[count], PAGE_SIZE - count, "not-defined:");
  25. else
  26. count += scnprintf(&buf[count], PAGE_SIZE - count, "%lld:", fps->trip_point);
  27. if (fps->speed == 0xFFFFFFFF)
  28. count += scnprintf(&buf[count], PAGE_SIZE - count, "not-defined:");
  29. else
  30. count += scnprintf(&buf[count], PAGE_SIZE - count, "%lld:", fps->speed);
  31. if (fps->noise_level == 0xFFFFFFFF)
  32. count += scnprintf(&buf[count], PAGE_SIZE - count, "not-defined:");
  33. else
  34. count += scnprintf(&buf[count], PAGE_SIZE - count, "%lld:", fps->noise_level * 100);
  35. if (fps->power == 0xFFFFFFFF)
  36. count += scnprintf(&buf[count], PAGE_SIZE - count, "not-defined\n");
  37. else
  38. count += scnprintf(&buf[count], PAGE_SIZE - count, "%lld\n", fps->power);
  39. return count;
  40. }
  41. static ssize_t show_fan_speed(struct device *dev, struct device_attribute *attr, char *buf)
  42. {
  43. struct acpi_device *acpi_dev = container_of(dev, struct acpi_device, dev);
  44. struct acpi_fan_fst fst;
  45. int status;
  46. status = acpi_fan_get_fst(acpi_dev, &fst);
  47. if (status)
  48. return status;
  49. return sprintf(buf, "%lld\n", fst.speed);
  50. }
  51. static ssize_t show_fine_grain_control(struct device *dev, struct device_attribute *attr, char *buf)
  52. {
  53. struct acpi_device *acpi_dev = container_of(dev, struct acpi_device, dev);
  54. struct acpi_fan *fan = acpi_driver_data(acpi_dev);
  55. return sprintf(buf, "%d\n", fan->fif.fine_grain_ctrl);
  56. }
  57. int acpi_fan_create_attributes(struct acpi_device *device)
  58. {
  59. struct acpi_fan *fan = acpi_driver_data(device);
  60. int i, status;
  61. sysfs_attr_init(&fan->fine_grain_control.attr);
  62. fan->fine_grain_control.show = show_fine_grain_control;
  63. fan->fine_grain_control.store = NULL;
  64. fan->fine_grain_control.attr.name = "fine_grain_control";
  65. fan->fine_grain_control.attr.mode = 0444;
  66. status = sysfs_create_file(&device->dev.kobj, &fan->fine_grain_control.attr);
  67. if (status)
  68. return status;
  69. /* _FST is present if we are here */
  70. sysfs_attr_init(&fan->fst_speed.attr);
  71. fan->fst_speed.show = show_fan_speed;
  72. fan->fst_speed.store = NULL;
  73. fan->fst_speed.attr.name = "fan_speed_rpm";
  74. fan->fst_speed.attr.mode = 0444;
  75. status = sysfs_create_file(&device->dev.kobj, &fan->fst_speed.attr);
  76. if (status)
  77. goto rem_fine_grain_attr;
  78. for (i = 0; i < fan->fps_count; ++i) {
  79. struct acpi_fan_fps *fps = &fan->fps[i];
  80. snprintf(fps->name, ACPI_FPS_NAME_LEN, "state%d", i);
  81. sysfs_attr_init(&fps->dev_attr.attr);
  82. fps->dev_attr.show = show_state;
  83. fps->dev_attr.store = NULL;
  84. fps->dev_attr.attr.name = fps->name;
  85. fps->dev_attr.attr.mode = 0444;
  86. status = sysfs_create_file(&device->dev.kobj, &fps->dev_attr.attr);
  87. if (status) {
  88. int j;
  89. for (j = 0; j < i; ++j)
  90. sysfs_remove_file(&device->dev.kobj, &fan->fps[j].dev_attr.attr);
  91. goto rem_fst_attr;
  92. }
  93. }
  94. return 0;
  95. rem_fst_attr:
  96. sysfs_remove_file(&device->dev.kobj, &fan->fst_speed.attr);
  97. rem_fine_grain_attr:
  98. sysfs_remove_file(&device->dev.kobj, &fan->fine_grain_control.attr);
  99. return status;
  100. }
  101. void acpi_fan_delete_attributes(struct acpi_device *device)
  102. {
  103. struct acpi_fan *fan = acpi_driver_data(device);
  104. int i;
  105. for (i = 0; i < fan->fps_count; ++i)
  106. sysfs_remove_file(&device->dev.kobj, &fan->fps[i].dev_attr.attr);
  107. sysfs_remove_file(&device->dev.kobj, &fan->fst_speed.attr);
  108. sysfs_remove_file(&device->dev.kobj, &fan->fine_grain_control.attr);
  109. }