int340x_thermal_zone.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. *temp = 0;
  127. else
  128. *temp = hyst * 100;
  129. return 0;
  130. }
  131. static struct thermal_zone_device_ops int340x_thermal_zone_ops = {
  132. .get_temp = int340x_thermal_get_zone_temp,
  133. .get_trip_temp = int340x_thermal_get_trip_temp,
  134. .get_trip_type = int340x_thermal_get_trip_type,
  135. .set_trip_temp = int340x_thermal_set_trip_temp,
  136. .get_trip_hyst = int340x_thermal_get_trip_hyst,
  137. };
  138. static int int340x_thermal_get_trip_config(acpi_handle handle, char *name,
  139. int *temp)
  140. {
  141. unsigned long long r;
  142. acpi_status status;
  143. status = acpi_evaluate_integer(handle, name, NULL, &r);
  144. if (ACPI_FAILURE(status))
  145. return -EIO;
  146. *temp = DECI_KELVIN_TO_MILLICELSIUS(r);
  147. return 0;
  148. }
  149. int int340x_thermal_read_trips(struct int34x_thermal_zone *int34x_zone)
  150. {
  151. int trip_cnt = int34x_zone->aux_trip_nr;
  152. int i;
  153. int34x_zone->crt_trip_id = -1;
  154. if (!int340x_thermal_get_trip_config(int34x_zone->adev->handle, "_CRT",
  155. &int34x_zone->crt_temp))
  156. int34x_zone->crt_trip_id = trip_cnt++;
  157. int34x_zone->hot_trip_id = -1;
  158. if (!int340x_thermal_get_trip_config(int34x_zone->adev->handle, "_HOT",
  159. &int34x_zone->hot_temp))
  160. int34x_zone->hot_trip_id = trip_cnt++;
  161. int34x_zone->psv_trip_id = -1;
  162. if (!int340x_thermal_get_trip_config(int34x_zone->adev->handle, "_PSV",
  163. &int34x_zone->psv_temp))
  164. int34x_zone->psv_trip_id = trip_cnt++;
  165. for (i = 0; i < INT340X_THERMAL_MAX_ACT_TRIP_COUNT; i++) {
  166. char name[5] = { '_', 'A', 'C', '0' + i, '\0' };
  167. if (int340x_thermal_get_trip_config(int34x_zone->adev->handle,
  168. name,
  169. &int34x_zone->act_trips[i].temp))
  170. break;
  171. int34x_zone->act_trips[i].id = trip_cnt++;
  172. int34x_zone->act_trips[i].valid = true;
  173. }
  174. return trip_cnt;
  175. }
  176. EXPORT_SYMBOL_GPL(int340x_thermal_read_trips);
  177. static struct thermal_zone_params int340x_thermal_params = {
  178. .governor_name = "user_space",
  179. .no_hwmon = true,
  180. };
  181. struct int34x_thermal_zone *int340x_thermal_zone_add(struct acpi_device *adev,
  182. struct thermal_zone_device_ops *override_ops)
  183. {
  184. struct int34x_thermal_zone *int34x_thermal_zone;
  185. acpi_status status;
  186. unsigned long long trip_cnt;
  187. int trip_mask = 0;
  188. int ret;
  189. int34x_thermal_zone = kzalloc(sizeof(*int34x_thermal_zone),
  190. GFP_KERNEL);
  191. if (!int34x_thermal_zone)
  192. return ERR_PTR(-ENOMEM);
  193. int34x_thermal_zone->adev = adev;
  194. int34x_thermal_zone->override_ops = override_ops;
  195. status = acpi_evaluate_integer(adev->handle, "PATC", NULL, &trip_cnt);
  196. if (ACPI_FAILURE(status))
  197. trip_cnt = 0;
  198. else {
  199. int34x_thermal_zone->aux_trips =
  200. kcalloc(trip_cnt,
  201. sizeof(*int34x_thermal_zone->aux_trips),
  202. GFP_KERNEL);
  203. if (!int34x_thermal_zone->aux_trips) {
  204. ret = -ENOMEM;
  205. goto err_trip_alloc;
  206. }
  207. trip_mask = BIT(trip_cnt) - 1;
  208. int34x_thermal_zone->aux_trip_nr = trip_cnt;
  209. }
  210. trip_cnt = int340x_thermal_read_trips(int34x_thermal_zone);
  211. int34x_thermal_zone->lpat_table = acpi_lpat_get_conversion_table(
  212. adev->handle);
  213. int34x_thermal_zone->zone = thermal_zone_device_register(
  214. acpi_device_bid(adev),
  215. trip_cnt,
  216. trip_mask, int34x_thermal_zone,
  217. &int340x_thermal_zone_ops,
  218. &int340x_thermal_params,
  219. 0, 0);
  220. if (IS_ERR(int34x_thermal_zone->zone)) {
  221. ret = PTR_ERR(int34x_thermal_zone->zone);
  222. goto err_thermal_zone;
  223. }
  224. return int34x_thermal_zone;
  225. err_thermal_zone:
  226. acpi_lpat_free_conversion_table(int34x_thermal_zone->lpat_table);
  227. kfree(int34x_thermal_zone->aux_trips);
  228. err_trip_alloc:
  229. kfree(int34x_thermal_zone);
  230. return ERR_PTR(ret);
  231. }
  232. EXPORT_SYMBOL_GPL(int340x_thermal_zone_add);
  233. void int340x_thermal_zone_remove(struct int34x_thermal_zone
  234. *int34x_thermal_zone)
  235. {
  236. thermal_zone_device_unregister(int34x_thermal_zone->zone);
  237. acpi_lpat_free_conversion_table(int34x_thermal_zone->lpat_table);
  238. kfree(int34x_thermal_zone->aux_trips);
  239. kfree(int34x_thermal_zone);
  240. }
  241. EXPORT_SYMBOL_GPL(int340x_thermal_zone_remove);
  242. MODULE_AUTHOR("Aaron Lu <aaron.lu@intel.com>");
  243. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  244. MODULE_DESCRIPTION("Intel INT340x common thermal zone handler");
  245. MODULE_LICENSE("GPL v2");