thermal.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <thermal.h>
  5. #include "thermal_nl.h"
  6. int for_each_thermal_cdev(struct thermal_cdev *cdev, cb_tc_t cb, void *arg)
  7. {
  8. int i, ret = 0;
  9. if (!cdev)
  10. return 0;
  11. for (i = 0; cdev[i].id != -1; i++)
  12. ret |= cb(&cdev[i], arg);
  13. return ret;
  14. }
  15. int for_each_thermal_trip(struct thermal_trip *tt, cb_tt_t cb, void *arg)
  16. {
  17. int i, ret = 0;
  18. if (!tt)
  19. return 0;
  20. for (i = 0; tt[i].id != -1; i++)
  21. ret |= cb(&tt[i], arg);
  22. return ret;
  23. }
  24. int for_each_thermal_zone(struct thermal_zone *tz, cb_tz_t cb, void *arg)
  25. {
  26. int i, ret = 0;
  27. if (!tz)
  28. return 0;
  29. for (i = 0; tz[i].id != -1; i++)
  30. ret |= cb(&tz[i], arg);
  31. return ret;
  32. }
  33. struct thermal_zone *thermal_zone_find_by_name(struct thermal_zone *tz,
  34. const char *name)
  35. {
  36. int i;
  37. if (!tz || !name)
  38. return NULL;
  39. for (i = 0; tz[i].id != -1; i++) {
  40. if (!strcmp(tz[i].name, name))
  41. return &tz[i];
  42. }
  43. return NULL;
  44. }
  45. struct thermal_zone *thermal_zone_find_by_id(struct thermal_zone *tz, int id)
  46. {
  47. int i;
  48. if (!tz || id < 0)
  49. return NULL;
  50. for (i = 0; tz[i].id != -1; i++) {
  51. if (tz[i].id == id)
  52. return &tz[i];
  53. }
  54. return NULL;
  55. }
  56. static int __thermal_zone_discover(struct thermal_zone *tz, void *th)
  57. {
  58. if (thermal_cmd_get_trip(th, tz) < 0)
  59. return -1;
  60. if (thermal_cmd_get_governor(th, tz))
  61. return -1;
  62. return 0;
  63. }
  64. struct thermal_zone *thermal_zone_discover(struct thermal_handler *th)
  65. {
  66. struct thermal_zone *tz;
  67. if (thermal_cmd_get_tz(th, &tz) < 0)
  68. return NULL;
  69. if (for_each_thermal_zone(tz, __thermal_zone_discover, th))
  70. return NULL;
  71. return tz;
  72. }
  73. void thermal_exit(struct thermal_handler *th)
  74. {
  75. thermal_cmd_exit(th);
  76. thermal_events_exit(th);
  77. thermal_sampling_exit(th);
  78. free(th);
  79. }
  80. struct thermal_handler *thermal_init(struct thermal_ops *ops)
  81. {
  82. struct thermal_handler *th;
  83. th = malloc(sizeof(*th));
  84. if (!th)
  85. return NULL;
  86. th->ops = ops;
  87. if (thermal_events_init(th))
  88. goto out_free;
  89. if (thermal_sampling_init(th))
  90. goto out_free;
  91. if (thermal_cmd_init(th))
  92. goto out_free;
  93. return th;
  94. out_free:
  95. free(th);
  96. return NULL;
  97. }