thermal_trip.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. guard(thermal_zone)(tz);
  42. return for_each_thermal_trip(tz, cb, data);
  43. }
  44. EXPORT_SYMBOL_GPL(thermal_zone_for_each_trip);
  45. void thermal_zone_set_trips(struct thermal_zone_device *tz, int low, int high)
  46. {
  47. int ret;
  48. lockdep_assert_held(&tz->lock);
  49. if (!tz->ops.set_trips)
  50. return;
  51. /* No need to change trip points */
  52. if (tz->prev_low_trip == low && tz->prev_high_trip == high)
  53. return;
  54. tz->prev_low_trip = low;
  55. tz->prev_high_trip = high;
  56. dev_dbg(&tz->device,
  57. "new temperature boundaries: %d < x < %d\n", low, high);
  58. /*
  59. * Set a temperature window. When this window is left the driver
  60. * must inform the thermal core via thermal_zone_device_update.
  61. */
  62. ret = tz->ops.set_trips(tz, low, high);
  63. if (ret)
  64. dev_err(&tz->device, "Failed to set trips: %d\n", ret);
  65. }
  66. int thermal_zone_trip_id(const struct thermal_zone_device *tz,
  67. const struct thermal_trip *trip)
  68. {
  69. /*
  70. * Assume the trip to be located within the bounds of the thermal
  71. * zone's trips[] table.
  72. */
  73. return trip_to_trip_desc(trip) - tz->trips;
  74. }