int340x_thermal_zone.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * int340x_thermal_zone.c
  3. * Copyright (c) 2015, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/acpi.h>
  19. #include <linux/thermal.h>
  20. #include "int340x_thermal_zone.h"
  21. static int int340x_thermal_get_zone_temp(struct thermal_zone_device *zone,
  22. int *temp)
  23. {
  24. struct int34x_thermal_zone *d = zone->devdata;
  25. unsigned long long tmp;
  26. acpi_status status;
  27. if (d->override_ops && d->override_ops->get_temp)
  28. return d->override_ops->get_temp(zone, temp);
  29. status = acpi_evaluate_integer(d->adev->handle, "_TMP", NULL, &tmp);
  30. if (ACPI_FAILURE(status))
  31. return -EIO;
  32. if (d->lpat_table) {
  33. int conv_temp;
  34. conv_temp = acpi_lpat_raw_to_temp(d->lpat_table, (int)tmp);
  35. if (conv_temp < 0)
  36. return conv_temp;
  37. *temp = (unsigned long)conv_temp * 10;
  38. } else
  39. /* _TMP returns the temperature in tenths of degrees Kelvin */
  40. *temp = DECI_KELVIN_TO_MILLICELSIUS(tmp);
  41. return 0;
  42. }
  43. static int int340x_thermal_get_trip_temp(struct thermal_zone_device *zone,
  44. int trip, int *temp)
  45. {
  46. struct int34x_thermal_zone *d = zone->devdata;
  47. int i;
  48. if (d->override_ops && d->override_ops->get_trip_temp)
  49. return d->override_ops->get_trip_temp(zone, trip, temp);
  50. if (trip < d->aux_trip_nr)
  51. *temp = d->aux_trips[trip];
  52. else if (trip == d->crt_trip_id)
  53. *temp = d->crt_temp;
  54. else if (trip == d->psv_trip_id)
  55. *temp = d->psv_temp;
  56. else if (trip == d->hot_trip_id)
  57. *temp = d->hot_temp;
  58. else {
  59. for (i = 0; i < INT340X_THERMAL_MAX_ACT_TRIP_COUNT; i++) {
  60. if (d->act_trips[i].valid &&
  61. d->act_trips[i].id == trip) {
  62. *temp = d->act_trips[i].temp;
  63. break;
  64. }
  65. }
  66. if (i == INT340X_THERMAL_MAX_ACT_TRIP_COUNT)
  67. return -EINVAL;
  68. }
  69. return 0;
  70. }
  71. static int int340x_thermal_get_trip_type(struct thermal_zone_device *zone,
  72. int trip,
  73. enum thermal_trip_type *type)
  74. {
  75. struct int34x_thermal_zone *d = zone->devdata;
  76. int i;
  77. if (d->override_ops && d->override_ops->get_trip_type)
  78. return d->override_ops->get_trip_type(zone, trip, type);
  79. if (trip < d->aux_trip_nr)
  80. *type = THERMAL_TRIP_PASSIVE;
  81. else if (trip == d->crt_trip_id)
  82. *type = THERMAL_TRIP_CRITICAL;
  83. else if (trip == d->hot_trip_id)
  84. *type = THERMAL_TRIP_HOT;
  85. else if (trip == d->psv_trip_id)
  86. *type = THERMAL_TRIP_PASSIVE;
  87. else {
  88. for (i = 0; i < INT340X_THERMAL_MAX_ACT_TRIP_COUNT; i++) {
  89. if (d->act_trips[i].valid &&
  90. d->act_trips[i].id == trip) {
  91. *type = THERMAL_TRIP_ACTIVE;
  92. break;
  93. }
  94. }
  95. if (i == INT340X_THERMAL_MAX_ACT_TRIP_COUNT)
  96. return -EINVAL;
  97. }
  98. return 0;
  99. }
  100. static int int340x_thermal_set_trip_temp(struct thermal_zone_device *zone,
  101. int trip, int temp)
  102. {
  103. struct int34x_thermal_zone *d = zone->devdata;
  104. acpi_status status;
  105. char name[10];
  106. if (d->override_ops && d->override_ops->set_trip_temp)
  107. return d->override_ops->set_trip_temp(zone, trip, temp);
  108. snprintf(name, sizeof(name), "PAT%d", trip);
  109. status = acpi_execute_simple_method(d->adev->handle, name,
  110. MILLICELSIUS_TO_DECI_KELVIN(temp));
  111. if (ACPI_FAILURE(status))
  112. return -EIO;
  113. d->aux_trips[trip] = temp;
  114. return 0;
  115. }
  116. static int int340x_thermal_get_trip_hyst(struct thermal_zone_device *zone,
  117. int trip, int *temp)
  118. {
  119. struct int34x_thermal_zone *d = zone->devdata;
  120. acpi_status status;
  121. unsigned long long hyst;
  122. if (d->override_ops && d->override_ops->get_trip_hyst)
  123. return d->override_ops->get_trip_hyst(zone, trip, temp);
  124. status = acpi_evaluate_integer(d->adev->handle, "GTSH", NULL, &hyst);
  125. if (ACPI_FAILURE(status))
  126. return -EIO;
  127. *temp = hyst * 100;
  128. return 0;
  129. }
  130. static struct thermal_zone_device_ops int340x_thermal_zone_ops = {
  131. .get_temp = int340x_thermal_get_zone_temp,
  132. .get_trip_temp = int340x_thermal_get_trip_temp,
  133. .get_trip_type = int340x_thermal_get_trip_type,
  134. .set_trip_temp = int340x_thermal_set_trip_temp,
  135. .get_trip_hyst = int340x_thermal_get_trip_hyst,
  136. };
  137. static int int340x_thermal_get_trip_config(acpi_handle handle, char *name,
  138. int *temp)
  139. {
  140. unsigned long long r;
  141. acpi_status status;
  142. status = acpi_evaluate_integer(handle, name, NULL, &r);
  143. if (ACPI_FAILURE(status))
  144. return -EIO;
  145. *temp = DECI_KELVIN_TO_MILLICELSIUS(r);
  146. return 0;
  147. }
  148. int int340x_thermal_read_trips(struct int34x_thermal_zone *int34x_zone)
  149. {
  150. int trip_cnt = int34x_zone->aux_trip_nr;
  151. int i;
  152. int34x_zone->crt_trip_id = -1;
  153. if (!int340x_thermal_get_trip_config(int34x_zone->adev->handle, "_CRT",
  154. &int34x_zone->crt_temp))
  155. int34x_zone->crt_trip_id = trip_cnt++;
  156. int34x_zone->hot_trip_id = -1;
  157. if (!int340x_thermal_get_trip_config(int34x_zone->adev->handle, "_HOT",
  158. &int34x_zone->hot_temp))
  159. int34x_zone->hot_trip_id = trip_cnt++;
  160. int34x_zone->psv_trip_id = -1;
  161. if (!int340x_thermal_get_trip_config(int34x_zone->adev->handle, "_PSV",
  162. &int34x_zone->psv_temp))
  163. int34x_zone->psv_trip_id = trip_cnt++;
  164. for (i = 0; i < INT340X_THERMAL_MAX_ACT_TRIP_COUNT; i++) {
  165. char name[5] = { '_', 'A', 'C', '0' + i, '\0' };
  166. if (int340x_thermal_get_trip_config(int34x_zone->adev->handle,
  167. name,
  168. &int34x_zone->act_trips[i].temp))
  169. break;
  170. int34x_zone->act_trips[i].id = trip_cnt++;
  171. int34x_zone->act_trips[i].valid = true;
  172. }
  173. return trip_cnt;
  174. }
  175. EXPORT_SYMBOL_GPL(int340x_thermal_read_trips);
  176. static struct thermal_zone_params int340x_thermal_params = {
  177. .governor_name = "user_space",
  178. .no_hwmon = true,
  179. };
  180. struct int34x_thermal_zone *int340x_thermal_zone_add(struct acpi_device *adev,
  181. struct thermal_zone_device_ops *override_ops)
  182. {
  183. struct int34x_thermal_zone *int34x_thermal_zone;
  184. acpi_status status;
  185. unsigned long long trip_cnt;
  186. int trip_mask = 0;
  187. int ret;
  188. int34x_thermal_zone = kzalloc(sizeof(*int34x_thermal_zone),
  189. GFP_KERNEL);
  190. if (!int34x_thermal_zone)
  191. return ERR_PTR(-ENOMEM);
  192. int34x_thermal_zone->adev = adev;
  193. int34x_thermal_zone->override_ops = override_ops;
  194. status = acpi_evaluate_integer(adev->handle, "PATC", NULL, &trip_cnt);
  195. if (ACPI_FAILURE(status))
  196. trip_cnt = 0;
  197. else {
  198. int34x_thermal_zone->aux_trips = kzalloc(
  199. sizeof(*int34x_thermal_zone->aux_trips) *
  200. trip_cnt, GFP_KERNEL);
  201. if (!int34x_thermal_zone->aux_trips) {
  202. ret = -ENOMEM;
  203. goto err_trip_alloc;
  204. }
  205. trip_mask = BIT(trip_cnt) - 1;
  206. int34x_thermal_zone->aux_trip_nr = trip_cnt;
  207. }
  208. trip_cnt = int340x_thermal_read_trips(int34x_thermal_zone);
  209. int34x_thermal_zone->lpat_table = acpi_lpat_get_conversion_table(
  210. adev->handle);
  211. int34x_thermal_zone->zone = thermal_zone_device_register(
  212. acpi_device_bid(adev),
  213. trip_cnt,
  214. trip_mask, int34x_thermal_zone,
  215. &int340x_thermal_zone_ops,
  216. &int340x_thermal_params,
  217. 0, 0);
  218. if (IS_ERR(int34x_thermal_zone->zone)) {
  219. ret = PTR_ERR(int34x_thermal_zone->zone);
  220. goto err_thermal_zone;
  221. }
  222. return int34x_thermal_zone;
  223. err_thermal_zone:
  224. acpi_lpat_free_conversion_table(int34x_thermal_zone->lpat_table);
  225. kfree(int34x_thermal_zone->aux_trips);
  226. err_trip_alloc:
  227. kfree(int34x_thermal_zone);
  228. return ERR_PTR(ret);
  229. }
  230. EXPORT_SYMBOL_GPL(int340x_thermal_zone_add);
  231. void int340x_thermal_zone_remove(struct int34x_thermal_zone
  232. *int34x_thermal_zone)
  233. {
  234. thermal_zone_device_unregister(int34x_thermal_zone->zone);
  235. acpi_lpat_free_conversion_table(int34x_thermal_zone->lpat_table);
  236. kfree(int34x_thermal_zone->aux_trips);
  237. kfree(int34x_thermal_zone);
  238. }
  239. EXPORT_SYMBOL_GPL(int340x_thermal_zone_remove);
  240. MODULE_AUTHOR("Aaron Lu <aaron.lu@intel.com>");
  241. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  242. MODULE_DESCRIPTION("Intel INT340x common thermal zone handler");
  243. MODULE_LICENSE("GPL v2");