int3402_thermal.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * INT3402 thermal driver for memory temperature reporting
  3. *
  4. * Copyright (C) 2014, Intel Corporation
  5. * Authors: Aaron Lu <aaron.lu@intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/acpi.h>
  15. #include <linux/thermal.h>
  16. #define ACPI_ACTIVE_COOLING_MAX_NR 10
  17. struct active_trip {
  18. unsigned long temp;
  19. int id;
  20. bool valid;
  21. };
  22. struct int3402_thermal_data {
  23. unsigned long *aux_trips;
  24. int aux_trip_nr;
  25. unsigned long psv_temp;
  26. int psv_trip_id;
  27. unsigned long crt_temp;
  28. int crt_trip_id;
  29. unsigned long hot_temp;
  30. int hot_trip_id;
  31. struct active_trip act_trips[ACPI_ACTIVE_COOLING_MAX_NR];
  32. acpi_handle *handle;
  33. };
  34. static int int3402_thermal_get_zone_temp(struct thermal_zone_device *zone,
  35. unsigned long *temp)
  36. {
  37. struct int3402_thermal_data *d = zone->devdata;
  38. unsigned long long tmp;
  39. acpi_status status;
  40. status = acpi_evaluate_integer(d->handle, "_TMP", NULL, &tmp);
  41. if (ACPI_FAILURE(status))
  42. return -ENODEV;
  43. /* _TMP returns the temperature in tenths of degrees Kelvin */
  44. *temp = DECI_KELVIN_TO_MILLICELSIUS(tmp);
  45. return 0;
  46. }
  47. static int int3402_thermal_get_trip_temp(struct thermal_zone_device *zone,
  48. int trip, unsigned long *temp)
  49. {
  50. struct int3402_thermal_data *d = zone->devdata;
  51. int i;
  52. if (trip < d->aux_trip_nr)
  53. *temp = d->aux_trips[trip];
  54. else if (trip == d->crt_trip_id)
  55. *temp = d->crt_temp;
  56. else if (trip == d->psv_trip_id)
  57. *temp = d->psv_temp;
  58. else if (trip == d->hot_trip_id)
  59. *temp = d->hot_temp;
  60. else {
  61. for (i = 0; i < ACPI_ACTIVE_COOLING_MAX_NR; i++) {
  62. if (d->act_trips[i].valid &&
  63. d->act_trips[i].id == trip) {
  64. *temp = d->act_trips[i].temp;
  65. break;
  66. }
  67. }
  68. if (i == ACPI_ACTIVE_COOLING_MAX_NR)
  69. return -EINVAL;
  70. }
  71. return 0;
  72. }
  73. static int int3402_thermal_get_trip_type(struct thermal_zone_device *zone,
  74. int trip, enum thermal_trip_type *type)
  75. {
  76. struct int3402_thermal_data *d = zone->devdata;
  77. int i;
  78. if (trip < d->aux_trip_nr)
  79. *type = THERMAL_TRIP_PASSIVE;
  80. else if (trip == d->crt_trip_id)
  81. *type = THERMAL_TRIP_CRITICAL;
  82. else if (trip == d->hot_trip_id)
  83. *type = THERMAL_TRIP_HOT;
  84. else if (trip == d->psv_trip_id)
  85. *type = THERMAL_TRIP_PASSIVE;
  86. else {
  87. for (i = 0; i < ACPI_ACTIVE_COOLING_MAX_NR; i++) {
  88. if (d->act_trips[i].valid &&
  89. d->act_trips[i].id == trip) {
  90. *type = THERMAL_TRIP_ACTIVE;
  91. break;
  92. }
  93. }
  94. if (i == ACPI_ACTIVE_COOLING_MAX_NR)
  95. return -EINVAL;
  96. }
  97. return 0;
  98. }
  99. static int int3402_thermal_set_trip_temp(struct thermal_zone_device *zone, int trip,
  100. unsigned long temp)
  101. {
  102. struct int3402_thermal_data *d = zone->devdata;
  103. acpi_status status;
  104. char name[10];
  105. snprintf(name, sizeof(name), "PAT%d", trip);
  106. status = acpi_execute_simple_method(d->handle, name,
  107. MILLICELSIUS_TO_DECI_KELVIN(temp));
  108. if (ACPI_FAILURE(status))
  109. return -EIO;
  110. d->aux_trips[trip] = temp;
  111. return 0;
  112. }
  113. static struct thermal_zone_device_ops int3402_thermal_zone_ops = {
  114. .get_temp = int3402_thermal_get_zone_temp,
  115. .get_trip_temp = int3402_thermal_get_trip_temp,
  116. .get_trip_type = int3402_thermal_get_trip_type,
  117. .set_trip_temp = int3402_thermal_set_trip_temp,
  118. };
  119. static struct thermal_zone_params int3402_thermal_params = {
  120. .governor_name = "user_space",
  121. .no_hwmon = true,
  122. };
  123. static int int3402_thermal_get_temp(acpi_handle handle, char *name,
  124. unsigned long *temp)
  125. {
  126. unsigned long long r;
  127. acpi_status status;
  128. status = acpi_evaluate_integer(handle, name, NULL, &r);
  129. if (ACPI_FAILURE(status))
  130. return -EIO;
  131. *temp = DECI_KELVIN_TO_MILLICELSIUS(r);
  132. return 0;
  133. }
  134. static int int3402_thermal_probe(struct platform_device *pdev)
  135. {
  136. struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
  137. struct int3402_thermal_data *d;
  138. struct thermal_zone_device *zone;
  139. acpi_status status;
  140. unsigned long long trip_cnt;
  141. int trip_mask = 0, i;
  142. if (!acpi_has_method(adev->handle, "_TMP"))
  143. return -ENODEV;
  144. d = devm_kzalloc(&pdev->dev, sizeof(*d), GFP_KERNEL);
  145. if (!d)
  146. return -ENOMEM;
  147. status = acpi_evaluate_integer(adev->handle, "PATC", NULL, &trip_cnt);
  148. if (ACPI_FAILURE(status))
  149. trip_cnt = 0;
  150. else {
  151. d->aux_trips = devm_kzalloc(&pdev->dev,
  152. sizeof(*d->aux_trips) * trip_cnt, GFP_KERNEL);
  153. if (!d->aux_trips)
  154. return -ENOMEM;
  155. trip_mask = trip_cnt - 1;
  156. d->handle = adev->handle;
  157. d->aux_trip_nr = trip_cnt;
  158. }
  159. d->crt_trip_id = -1;
  160. if (!int3402_thermal_get_temp(adev->handle, "_CRT", &d->crt_temp))
  161. d->crt_trip_id = trip_cnt++;
  162. d->hot_trip_id = -1;
  163. if (!int3402_thermal_get_temp(adev->handle, "_HOT", &d->hot_temp))
  164. d->hot_trip_id = trip_cnt++;
  165. d->psv_trip_id = -1;
  166. if (!int3402_thermal_get_temp(adev->handle, "_PSV", &d->psv_temp))
  167. d->psv_trip_id = trip_cnt++;
  168. for (i = 0; i < ACPI_ACTIVE_COOLING_MAX_NR; i++) {
  169. char name[5] = { '_', 'A', 'C', '0' + i, '\0' };
  170. if (int3402_thermal_get_temp(adev->handle, name,
  171. &d->act_trips[i].temp))
  172. break;
  173. d->act_trips[i].id = trip_cnt++;
  174. d->act_trips[i].valid = true;
  175. }
  176. zone = thermal_zone_device_register(acpi_device_bid(adev), trip_cnt,
  177. trip_mask, d,
  178. &int3402_thermal_zone_ops,
  179. &int3402_thermal_params,
  180. 0, 0);
  181. if (IS_ERR(zone))
  182. return PTR_ERR(zone);
  183. platform_set_drvdata(pdev, zone);
  184. return 0;
  185. }
  186. static int int3402_thermal_remove(struct platform_device *pdev)
  187. {
  188. struct thermal_zone_device *zone = platform_get_drvdata(pdev);
  189. thermal_zone_device_unregister(zone);
  190. return 0;
  191. }
  192. static const struct acpi_device_id int3402_thermal_match[] = {
  193. {"INT3402", 0},
  194. {}
  195. };
  196. MODULE_DEVICE_TABLE(acpi, int3402_thermal_match);
  197. static struct platform_driver int3402_thermal_driver = {
  198. .probe = int3402_thermal_probe,
  199. .remove = int3402_thermal_remove,
  200. .driver = {
  201. .name = "int3402 thermal",
  202. .acpi_match_table = int3402_thermal_match,
  203. },
  204. };
  205. module_platform_driver(int3402_thermal_driver);
  206. MODULE_DESCRIPTION("INT3402 Thermal driver");
  207. MODULE_LICENSE("GPL");