int340x_thermal_zone.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. static struct thermal_zone_params int340x_thermal_params = {
  149. .governor_name = "user_space",
  150. .no_hwmon = true,
  151. };
  152. struct int34x_thermal_zone *int340x_thermal_zone_add(struct acpi_device *adev,
  153. struct thermal_zone_device_ops *override_ops)
  154. {
  155. struct int34x_thermal_zone *int34x_thermal_zone;
  156. acpi_status status;
  157. unsigned long long trip_cnt;
  158. int trip_mask = 0, i;
  159. int ret;
  160. int34x_thermal_zone = kzalloc(sizeof(*int34x_thermal_zone),
  161. GFP_KERNEL);
  162. if (!int34x_thermal_zone)
  163. return ERR_PTR(-ENOMEM);
  164. int34x_thermal_zone->adev = adev;
  165. int34x_thermal_zone->override_ops = override_ops;
  166. status = acpi_evaluate_integer(adev->handle, "PATC", NULL, &trip_cnt);
  167. if (ACPI_FAILURE(status))
  168. trip_cnt = 0;
  169. else {
  170. int34x_thermal_zone->aux_trips = kzalloc(
  171. sizeof(*int34x_thermal_zone->aux_trips) *
  172. trip_cnt, GFP_KERNEL);
  173. if (!int34x_thermal_zone->aux_trips) {
  174. ret = -ENOMEM;
  175. goto err_trip_alloc;
  176. }
  177. trip_mask = BIT(trip_cnt) - 1;
  178. int34x_thermal_zone->aux_trip_nr = trip_cnt;
  179. }
  180. int34x_thermal_zone->crt_trip_id = -1;
  181. if (!int340x_thermal_get_trip_config(adev->handle, "_CRT",
  182. &int34x_thermal_zone->crt_temp))
  183. int34x_thermal_zone->crt_trip_id = trip_cnt++;
  184. int34x_thermal_zone->hot_trip_id = -1;
  185. if (!int340x_thermal_get_trip_config(adev->handle, "_HOT",
  186. &int34x_thermal_zone->hot_temp))
  187. int34x_thermal_zone->hot_trip_id = trip_cnt++;
  188. int34x_thermal_zone->psv_trip_id = -1;
  189. if (!int340x_thermal_get_trip_config(adev->handle, "_PSV",
  190. &int34x_thermal_zone->psv_temp))
  191. int34x_thermal_zone->psv_trip_id = trip_cnt++;
  192. for (i = 0; i < INT340X_THERMAL_MAX_ACT_TRIP_COUNT; i++) {
  193. char name[5] = { '_', 'A', 'C', '0' + i, '\0' };
  194. if (int340x_thermal_get_trip_config(adev->handle, name,
  195. &int34x_thermal_zone->act_trips[i].temp))
  196. break;
  197. int34x_thermal_zone->act_trips[i].id = trip_cnt++;
  198. int34x_thermal_zone->act_trips[i].valid = true;
  199. }
  200. int34x_thermal_zone->lpat_table = acpi_lpat_get_conversion_table(
  201. adev->handle);
  202. int34x_thermal_zone->zone = thermal_zone_device_register(
  203. acpi_device_bid(adev),
  204. trip_cnt,
  205. trip_mask, int34x_thermal_zone,
  206. &int340x_thermal_zone_ops,
  207. &int340x_thermal_params,
  208. 0, 0);
  209. if (IS_ERR(int34x_thermal_zone->zone)) {
  210. ret = PTR_ERR(int34x_thermal_zone->zone);
  211. goto err_thermal_zone;
  212. }
  213. return int34x_thermal_zone;
  214. err_thermal_zone:
  215. acpi_lpat_free_conversion_table(int34x_thermal_zone->lpat_table);
  216. kfree(int34x_thermal_zone->aux_trips);
  217. err_trip_alloc:
  218. kfree(int34x_thermal_zone);
  219. return ERR_PTR(ret);
  220. }
  221. EXPORT_SYMBOL_GPL(int340x_thermal_zone_add);
  222. void int340x_thermal_zone_remove(struct int34x_thermal_zone
  223. *int34x_thermal_zone)
  224. {
  225. thermal_zone_device_unregister(int34x_thermal_zone->zone);
  226. acpi_lpat_free_conversion_table(int34x_thermal_zone->lpat_table);
  227. kfree(int34x_thermal_zone->aux_trips);
  228. kfree(int34x_thermal_zone);
  229. }
  230. EXPORT_SYMBOL_GPL(int340x_thermal_zone_remove);
  231. MODULE_AUTHOR("Aaron Lu <aaron.lu@intel.com>");
  232. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  233. MODULE_DESCRIPTION("Intel INT340x common thermal zone handler");
  234. MODULE_LICENSE("GPL v2");