thermal.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: LGPL-2.1+
  2. // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
  3. #include <stdio.h>
  4. #include <limits.h>
  5. #include <thermal.h>
  6. #include "thermal_nl.h"
  7. int for_each_thermal_threshold(struct thermal_threshold *th, cb_th_t cb, void *arg)
  8. {
  9. int i, ret = 0;
  10. if (!th)
  11. return 0;
  12. for (i = 0; th[i].temperature != INT_MAX; i++)
  13. ret |= cb(&th[i], arg);
  14. return ret;
  15. }
  16. int for_each_thermal_cdev(struct thermal_cdev *cdev, cb_tc_t cb, void *arg)
  17. {
  18. int i, ret = 0;
  19. if (!cdev)
  20. return 0;
  21. for (i = 0; cdev[i].id != -1; i++)
  22. ret |= cb(&cdev[i], arg);
  23. return ret;
  24. }
  25. int for_each_thermal_trip(struct thermal_trip *tt, cb_tt_t cb, void *arg)
  26. {
  27. int i, ret = 0;
  28. if (!tt)
  29. return 0;
  30. for (i = 0; tt[i].id != -1; i++)
  31. ret |= cb(&tt[i], arg);
  32. return ret;
  33. }
  34. int for_each_thermal_zone(struct thermal_zone *tz, cb_tz_t cb, void *arg)
  35. {
  36. int i, ret = 0;
  37. if (!tz)
  38. return 0;
  39. for (i = 0; tz[i].id != -1; i++)
  40. ret |= cb(&tz[i], arg);
  41. return ret;
  42. }
  43. struct thermal_zone *thermal_zone_find_by_name(struct thermal_zone *tz,
  44. const char *name)
  45. {
  46. int i;
  47. if (!tz || !name)
  48. return NULL;
  49. for (i = 0; tz[i].id != -1; i++) {
  50. if (!strcmp(tz[i].name, name))
  51. return &tz[i];
  52. }
  53. return NULL;
  54. }
  55. struct thermal_zone *thermal_zone_find_by_id(struct thermal_zone *tz, int id)
  56. {
  57. int i;
  58. if (!tz || id < 0)
  59. return NULL;
  60. for (i = 0; tz[i].id != -1; i++) {
  61. if (tz[i].id == id)
  62. return &tz[i];
  63. }
  64. return NULL;
  65. }
  66. static int __thermal_zone_discover(struct thermal_zone *tz, void *th)
  67. {
  68. if (thermal_cmd_get_trip(th, tz) < 0)
  69. return -1;
  70. if (thermal_cmd_threshold_get(th, tz))
  71. return -1;
  72. if (thermal_cmd_get_governor(th, tz))
  73. return -1;
  74. return 0;
  75. }
  76. struct thermal_zone *thermal_zone_discover(struct thermal_handler *th)
  77. {
  78. struct thermal_zone *tz;
  79. if (thermal_cmd_get_tz(th, &tz) < 0)
  80. return NULL;
  81. if (for_each_thermal_zone(tz, __thermal_zone_discover, th))
  82. return NULL;
  83. return tz;
  84. }
  85. void thermal_exit(struct thermal_handler *th)
  86. {
  87. thermal_cmd_exit(th);
  88. thermal_events_exit(th);
  89. thermal_sampling_exit(th);
  90. free(th);
  91. }
  92. struct thermal_handler *thermal_init(struct thermal_ops *ops)
  93. {
  94. struct thermal_handler *th;
  95. th = malloc(sizeof(*th));
  96. if (!th)
  97. return NULL;
  98. th->ops = ops;
  99. if (thermal_events_init(th))
  100. goto out_free;
  101. if (thermal_sampling_init(th))
  102. goto out_free;
  103. if (thermal_cmd_init(th))
  104. goto out_free;
  105. return th;
  106. out_free:
  107. free(th);
  108. return NULL;
  109. }