thermal_trip.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2008 Intel Corp
  4. * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
  5. * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
  6. * Copyright 2022 Linaro Limited
  7. *
  8. * Thermal trips handling
  9. */
  10. #include "thermal_core.h"
  11. static const char *trip_type_names[] = {
  12. [THERMAL_TRIP_ACTIVE] = "active",
  13. [THERMAL_TRIP_PASSIVE] = "passive",
  14. [THERMAL_TRIP_HOT] = "hot",
  15. [THERMAL_TRIP_CRITICAL] = "critical",
  16. };
  17. const char *thermal_trip_type_name(enum thermal_trip_type trip_type)
  18. {
  19. if (trip_type < THERMAL_TRIP_ACTIVE || trip_type > THERMAL_TRIP_CRITICAL)
  20. return "unknown";
  21. return trip_type_names[trip_type];
  22. }
  23. int for_each_thermal_trip(struct thermal_zone_device *tz,
  24. int (*cb)(struct thermal_trip *, void *),
  25. void *data)
  26. {
  27. struct thermal_trip_desc *td;
  28. int ret;
  29. for_each_trip_desc(tz, td) {
  30. ret = cb(&td->trip, data);
  31. if (ret)
  32. return ret;
  33. }
  34. return 0;
  35. }
  36. EXPORT_SYMBOL_GPL(for_each_thermal_trip);
  37. int thermal_zone_for_each_trip(struct thermal_zone_device *tz,
  38. int (*cb)(struct thermal_trip *, void *),
  39. void *data)
  40. {
  41. int ret;
  42. mutex_lock(&tz->lock);
  43. ret = for_each_thermal_trip(tz, cb, data);
  44. mutex_unlock(&tz->lock);
  45. return ret;
  46. }
  47. EXPORT_SYMBOL_GPL(thermal_zone_for_each_trip);
  48. void thermal_zone_set_trips(struct thermal_zone_device *tz, int low, int high)
  49. {
  50. int ret;
  51. lockdep_assert_held(&tz->lock);
  52. if (!tz->ops.set_trips)
  53. return;
  54. /* No need to change trip points */
  55. if (tz->prev_low_trip == low && tz->prev_high_trip == high)
  56. return;
  57. tz->prev_low_trip = low;
  58. tz->prev_high_trip = high;
  59. dev_dbg(&tz->device,
  60. "new temperature boundaries: %d < x < %d\n", low, high);
  61. /*
  62. * Set a temperature window. When this window is left the driver
  63. * must inform the thermal core via thermal_zone_device_update.
  64. */
  65. ret = tz->ops.set_trips(tz, low, high);
  66. if (ret)
  67. dev_err(&tz->device, "Failed to set trips: %d\n", ret);
  68. }
  69. int thermal_zone_trip_id(const struct thermal_zone_device *tz,
  70. const struct thermal_trip *trip)
  71. {
  72. /*
  73. * Assume the trip to be located within the bounds of the thermal
  74. * zone's trips[] table.
  75. */
  76. return trip_to_trip_desc(trip) - tz->trips;
  77. }
  78. void thermal_zone_set_trip_hyst(struct thermal_zone_device *tz,
  79. struct thermal_trip *trip, int hyst)
  80. {
  81. WRITE_ONCE(trip->hysteresis, hyst);
  82. thermal_notify_tz_trip_change(tz, trip);
  83. }
  84. void thermal_zone_set_trip_temp(struct thermal_zone_device *tz,
  85. struct thermal_trip *trip, int temp)
  86. {
  87. if (trip->temperature == temp)
  88. return;
  89. WRITE_ONCE(trip->temperature, temp);
  90. thermal_notify_tz_trip_change(tz, trip);
  91. if (temp == THERMAL_TEMP_INVALID) {
  92. struct thermal_trip_desc *td = trip_to_trip_desc(trip);
  93. if (tz->temperature >= td->threshold) {
  94. /*
  95. * The trip has been crossed on the way up, so some
  96. * adjustments are needed to compensate for the lack
  97. * of it going forward.
  98. */
  99. if (trip->type == THERMAL_TRIP_PASSIVE) {
  100. tz->passive--;
  101. WARN_ON_ONCE(tz->passive < 0);
  102. }
  103. thermal_zone_trip_down(tz, trip);
  104. }
  105. /*
  106. * Invalidate the threshold to avoid triggering a spurious
  107. * trip crossing notification when the trip becomes valid.
  108. */
  109. td->threshold = INT_MAX;
  110. }
  111. }
  112. EXPORT_SYMBOL_GPL(thermal_zone_set_trip_temp);