thermal.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-3-08 GuEe-GUI the first version
  9. */
  10. #ifndef __THERMAL_H__
  11. #define __THERMAL_H__
  12. #include <rtdef.h>
  13. #include <dt-bindings/thermal/thermal.h>
  14. /* No upper/lower limit requirement */
  15. #define RT_THERMAL_NO_LIMIT ((rt_uint32_t)THERMAL_NO_LIMIT)
  16. #define RT_THERMAL_TEMP_INVALID (-274000)
  17. struct rt_thermal_zone_ops;
  18. struct rt_thermal_cooling_device;
  19. struct rt_thermal_cooling_device_ops;
  20. struct rt_thermal_cooling_governor;
  21. enum rt_thermal_trip_type
  22. {
  23. RT_THERMAL_TRIP_ACTIVE = 0,
  24. RT_THERMAL_TRIP_PASSIVE,
  25. RT_THERMAL_TRIP_HOT,
  26. RT_THERMAL_TRIP_CRITICAL,
  27. RT_THERMAL_TRIP_TYPE_MAX,
  28. };
  29. struct rt_thermal_trip
  30. {
  31. /* Temperature value in millidegree celsius */
  32. int temperature;
  33. /* Relative hysteresis in millidegree celsius */
  34. int hysteresis;
  35. enum rt_thermal_trip_type type;
  36. void *priv;
  37. };
  38. struct rt_thermal_zone_params
  39. {
  40. /* Sustainable power (heat) that this thermal zone can dissipate in mW */
  41. int sustainable_power;
  42. /* Slope of a linear temperature adjustment curve */
  43. int slope;
  44. /* Offset of a linear temperature adjustment curve */
  45. int offset;
  46. };
  47. struct rt_thermal_cooling_cell
  48. {
  49. struct rt_thermal_cooling_device *cooling_devices;
  50. rt_uint32_t level_range[2];
  51. };
  52. struct rt_thermal_cooling_map
  53. {
  54. rt_uint32_t contribution;
  55. rt_size_t cells_nr;
  56. struct rt_thermal_cooling_cell *cells;
  57. struct rt_thermal_trip *trips;
  58. };
  59. struct rt_thermal_zone_device
  60. {
  61. struct rt_device parent;
  62. int zone_id;
  63. const struct rt_thermal_zone_ops *ops;
  64. rt_bool_t trips_free;
  65. rt_size_t trips_nr;
  66. struct rt_thermal_trip *trips;
  67. struct rt_thermal_zone_params params;
  68. rt_bool_t enabled;
  69. rt_bool_t cooling;
  70. int temperature;
  71. int last_temperature;
  72. int prev_low_trip;
  73. int prev_high_trip;
  74. rt_list_t notifier_nodes;
  75. struct rt_spinlock nodes_lock;
  76. rt_size_t cooling_maps_nr;
  77. struct rt_thermal_cooling_map *cooling_maps;
  78. rt_tick_t passive_delay, polling_delay;
  79. struct rt_work poller;
  80. struct rt_mutex mutex;
  81. void *priv;
  82. };
  83. struct rt_thermal_zone_ops
  84. {
  85. rt_err_t (*get_temp)(struct rt_thermal_zone_device *zdev, int *out_temp);
  86. rt_err_t (*set_trips)(struct rt_thermal_zone_device *zdev, int low_temp, int high_temp);
  87. rt_err_t (*set_trip_temp)(struct rt_thermal_zone_device *zdev, int trip_id, int temp);
  88. rt_err_t (*set_trip_hyst)(struct rt_thermal_zone_device *zdev, int trip_id, int hyst);
  89. void (*hot)(struct rt_thermal_zone_device *zdev);
  90. void (*critical)(struct rt_thermal_zone_device *zdev);
  91. };
  92. /*
  93. * We don't want to make a temperature control system
  94. * that is finer than an air conditioner's temperature control,
  95. * just ensure get a reliable heat dissipation under high-load task
  96. * or when the SoC temperature is too high.
  97. */
  98. struct rt_thermal_cooling_device
  99. {
  100. struct rt_device parent;
  101. const struct rt_thermal_cooling_device_ops *ops;
  102. /* The cooling capacity indicator */
  103. rt_ubase_t max_level;
  104. rt_list_t governor_node;
  105. struct rt_thermal_cooling_governor *gov;
  106. void *priv;
  107. };
  108. struct rt_thermal_cooling_device_ops
  109. {
  110. rt_err_t (*bind)(struct rt_thermal_cooling_device *cdev, struct rt_thermal_zone_device *zdev);
  111. rt_err_t (*unbind)(struct rt_thermal_cooling_device *cdev, struct rt_thermal_zone_device *zdev);
  112. rt_err_t (*get_max_level)(struct rt_thermal_cooling_device *cdev, rt_ubase_t *out_level);
  113. rt_err_t (*get_cur_level)(struct rt_thermal_cooling_device *cdev, rt_ubase_t *out_level);
  114. rt_err_t (*set_cur_level)(struct rt_thermal_cooling_device *cdev, rt_ubase_t level);
  115. };
  116. struct rt_thermal_cooling_governor
  117. {
  118. rt_list_t list;
  119. const char *name;
  120. rt_list_t cdev_nodes;
  121. void (*tuning)(struct rt_thermal_zone_device *zdev,
  122. int map_idx, int cell_idx, rt_ubase_t *level);
  123. };
  124. struct rt_thermal_notifier;
  125. #define RT_THERMAL_MSG_EVENT_UNSPECIFIED RT_BIT(0) /* Unspecified event */
  126. #define RT_THERMAL_MSG_EVENT_TEMP_SAMPLE RT_BIT(1) /* New Temperature sample */
  127. #define RT_THERMAL_MSG_TRIP_VIOLATED RT_BIT(2) /* TRIP Point violation */
  128. #define RT_THERMAL_MSG_TRIP_CHANGED RT_BIT(3) /* TRIP Point temperature changed */
  129. #define RT_THERMAL_MSG_DEVICE_DOWN RT_BIT(4) /* Thermal device is down */
  130. #define RT_THERMAL_MSG_DEVICE_UP RT_BIT(5) /* Thermal device is up after a down event */
  131. #define RT_THERMAL_MSG_DEVICE_POWER_CAPABILITY_CHANGED RT_BIT(6) /* Power capability changed */
  132. #define RT_THERMAL_MSG_TABLE_CHANGED RT_BIT(7) /* Thermal table(s) changed */
  133. #define RT_THERMAL_MSG_EVENT_KEEP_ALIVE RT_BIT(8) /* Request for user space handler to respond */
  134. typedef rt_err_t (*rt_thermal_notifier_callback)(struct rt_thermal_notifier *notifier,
  135. rt_ubase_t msg);
  136. struct rt_thermal_notifier
  137. {
  138. rt_list_t list;
  139. struct rt_thermal_zone_device *zdev;
  140. rt_thermal_notifier_callback callback;
  141. void *priv;
  142. };
  143. rt_err_t rt_thermal_zone_device_register(struct rt_thermal_zone_device *zdev);
  144. rt_err_t rt_thermal_zone_device_unregister(struct rt_thermal_zone_device *zdev);
  145. rt_err_t rt_thermal_cooling_device_register(struct rt_thermal_cooling_device *cdev);
  146. rt_err_t rt_thermal_cooling_device_unregister(struct rt_thermal_cooling_device *cdev);
  147. rt_err_t rt_thermal_cooling_governor_register(struct rt_thermal_cooling_governor *gov);
  148. rt_err_t rt_thermal_cooling_governor_unregister(struct rt_thermal_cooling_governor *gov);
  149. rt_err_t rt_thermal_cooling_device_change_governor(struct rt_thermal_cooling_device *cdev,
  150. const char *name);
  151. rt_err_t rt_thermal_zone_notifier_register(struct rt_thermal_zone_device *zdev,
  152. struct rt_thermal_notifier *notifier);
  153. rt_err_t rt_thermal_zone_notifier_unregister(struct rt_thermal_zone_device *zdev,
  154. struct rt_thermal_notifier *notifier);
  155. void rt_thermal_zone_device_update(struct rt_thermal_zone_device *zdev, rt_ubase_t msg);
  156. void rt_thermal_cooling_device_kick(struct rt_thermal_zone_device *zdev);
  157. rt_err_t rt_thermal_zone_set_trip(struct rt_thermal_zone_device *zdev, int trip_id,
  158. const struct rt_thermal_trip *trip);
  159. rt_err_t rt_thermal_zone_get_trip(struct rt_thermal_zone_device *zdev, int trip_id,
  160. struct rt_thermal_trip *out_trip);
  161. #endif /* __THERMAL_H__ */