int3400_thermal.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * INT3400 thermal driver
  3. *
  4. * Copyright (C) 2014, Intel Corporation
  5. * Authors: Zhang Rui <rui.zhang@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. #include "acpi_thermal_rel.h"
  17. enum int3400_thermal_uuid {
  18. INT3400_THERMAL_PASSIVE_1,
  19. INT3400_THERMAL_PASSIVE_2,
  20. INT3400_THERMAL_ACTIVE,
  21. INT3400_THERMAL_CRITICAL,
  22. INT3400_THERMAL_COOLING_MODE,
  23. INT3400_THERMAL_MAXIMUM_UUID,
  24. };
  25. static u8 *int3400_thermal_uuids[INT3400_THERMAL_MAXIMUM_UUID] = {
  26. "42A441D6-AE6A-462b-A84B-4A8CE79027D3",
  27. "9E04115A-AE87-4D1C-9500-0F3E340BFE75",
  28. "3A95C389-E4B8-4629-A526-C52C88626BAE",
  29. "97C68AE7-15FA-499c-B8C9-5DA81D606E0A",
  30. "16CAF1B7-DD38-40ed-B1C1-1B8A1913D531",
  31. };
  32. struct int3400_thermal_priv {
  33. struct acpi_device *adev;
  34. struct thermal_zone_device *thermal;
  35. int mode;
  36. int art_count;
  37. struct art *arts;
  38. int trt_count;
  39. struct trt *trts;
  40. u8 uuid_bitmap;
  41. int rel_misc_dev_res;
  42. };
  43. static int int3400_thermal_get_uuids(struct int3400_thermal_priv *priv)
  44. {
  45. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL};
  46. union acpi_object *obja, *objb;
  47. int i, j;
  48. int result = 0;
  49. acpi_status status;
  50. status = acpi_evaluate_object(priv->adev->handle, "IDSP", NULL, &buf);
  51. if (ACPI_FAILURE(status))
  52. return -ENODEV;
  53. obja = (union acpi_object *)buf.pointer;
  54. if (obja->type != ACPI_TYPE_PACKAGE) {
  55. result = -EINVAL;
  56. goto end;
  57. }
  58. for (i = 0; i < obja->package.count; i++) {
  59. objb = &obja->package.elements[i];
  60. if (objb->type != ACPI_TYPE_BUFFER) {
  61. result = -EINVAL;
  62. goto end;
  63. }
  64. /* UUID must be 16 bytes */
  65. if (objb->buffer.length != 16) {
  66. result = -EINVAL;
  67. goto end;
  68. }
  69. for (j = 0; j < INT3400_THERMAL_MAXIMUM_UUID; j++) {
  70. u8 uuid[16];
  71. acpi_str_to_uuid(int3400_thermal_uuids[j], uuid);
  72. if (!strncmp(uuid, objb->buffer.pointer, 16)) {
  73. priv->uuid_bitmap |= (1 << j);
  74. break;
  75. }
  76. }
  77. }
  78. end:
  79. kfree(buf.pointer);
  80. return result;
  81. }
  82. static int int3400_thermal_run_osc(acpi_handle handle,
  83. enum int3400_thermal_uuid uuid, bool enable)
  84. {
  85. u32 ret, buf[2];
  86. acpi_status status;
  87. int result = 0;
  88. struct acpi_osc_context context = {
  89. .uuid_str = int3400_thermal_uuids[uuid],
  90. .rev = 1,
  91. .cap.length = 8,
  92. };
  93. buf[OSC_QUERY_DWORD] = 0;
  94. buf[OSC_SUPPORT_DWORD] = enable;
  95. context.cap.pointer = buf;
  96. status = acpi_run_osc(handle, &context);
  97. if (ACPI_SUCCESS(status)) {
  98. ret = *((u32 *)(context.ret.pointer + 4));
  99. if (ret != enable)
  100. result = -EPERM;
  101. } else
  102. result = -EPERM;
  103. kfree(context.ret.pointer);
  104. return result;
  105. }
  106. static int int3400_thermal_get_temp(struct thermal_zone_device *thermal,
  107. unsigned long *temp)
  108. {
  109. *temp = 20 * 1000; /* faked temp sensor with 20C */
  110. return 0;
  111. }
  112. static int int3400_thermal_get_mode(struct thermal_zone_device *thermal,
  113. enum thermal_device_mode *mode)
  114. {
  115. struct int3400_thermal_priv *priv = thermal->devdata;
  116. if (!priv)
  117. return -EINVAL;
  118. *mode = priv->mode;
  119. return 0;
  120. }
  121. static int int3400_thermal_set_mode(struct thermal_zone_device *thermal,
  122. enum thermal_device_mode mode)
  123. {
  124. struct int3400_thermal_priv *priv = thermal->devdata;
  125. bool enable;
  126. int result = 0;
  127. if (!priv)
  128. return -EINVAL;
  129. if (mode == THERMAL_DEVICE_ENABLED)
  130. enable = true;
  131. else if (mode == THERMAL_DEVICE_DISABLED)
  132. enable = false;
  133. else
  134. return -EINVAL;
  135. if (enable != priv->mode) {
  136. priv->mode = enable;
  137. /* currently, only PASSIVE COOLING is supported */
  138. result = int3400_thermal_run_osc(priv->adev->handle,
  139. INT3400_THERMAL_PASSIVE_1, enable);
  140. }
  141. return result;
  142. }
  143. static struct thermal_zone_device_ops int3400_thermal_ops = {
  144. .get_temp = int3400_thermal_get_temp,
  145. };
  146. static struct thermal_zone_params int3400_thermal_params = {
  147. .governor_name = "user_space",
  148. .no_hwmon = true,
  149. };
  150. static int int3400_thermal_probe(struct platform_device *pdev)
  151. {
  152. struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
  153. struct int3400_thermal_priv *priv;
  154. int result;
  155. if (!adev)
  156. return -ENODEV;
  157. priv = kzalloc(sizeof(struct int3400_thermal_priv), GFP_KERNEL);
  158. if (!priv)
  159. return -ENOMEM;
  160. priv->adev = adev;
  161. result = int3400_thermal_get_uuids(priv);
  162. if (result)
  163. goto free_priv;
  164. result = acpi_parse_art(priv->adev->handle, &priv->art_count,
  165. &priv->arts, true);
  166. if (result)
  167. goto free_priv;
  168. result = acpi_parse_trt(priv->adev->handle, &priv->trt_count,
  169. &priv->trts, true);
  170. if (result)
  171. goto free_art;
  172. platform_set_drvdata(pdev, priv);
  173. if (priv->uuid_bitmap & 1 << INT3400_THERMAL_PASSIVE_1) {
  174. int3400_thermal_ops.get_mode = int3400_thermal_get_mode;
  175. int3400_thermal_ops.set_mode = int3400_thermal_set_mode;
  176. }
  177. priv->thermal = thermal_zone_device_register("INT3400 Thermal", 0, 0,
  178. priv, &int3400_thermal_ops,
  179. &int3400_thermal_params, 0, 0);
  180. if (IS_ERR(priv->thermal)) {
  181. result = PTR_ERR(priv->thermal);
  182. goto free_trt;
  183. }
  184. priv->rel_misc_dev_res = acpi_thermal_rel_misc_device_add(
  185. priv->adev->handle);
  186. return 0;
  187. free_trt:
  188. kfree(priv->trts);
  189. free_art:
  190. kfree(priv->arts);
  191. free_priv:
  192. kfree(priv);
  193. return result;
  194. }
  195. static int int3400_thermal_remove(struct platform_device *pdev)
  196. {
  197. struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
  198. if (!priv->rel_misc_dev_res)
  199. acpi_thermal_rel_misc_device_remove(priv->adev->handle);
  200. thermal_zone_device_unregister(priv->thermal);
  201. kfree(priv->trts);
  202. kfree(priv->arts);
  203. kfree(priv);
  204. return 0;
  205. }
  206. static const struct acpi_device_id int3400_thermal_match[] = {
  207. {"INT3400", 0},
  208. {}
  209. };
  210. MODULE_DEVICE_TABLE(acpi, int3400_thermal_match);
  211. static struct platform_driver int3400_thermal_driver = {
  212. .probe = int3400_thermal_probe,
  213. .remove = int3400_thermal_remove,
  214. .driver = {
  215. .name = "int3400 thermal",
  216. .owner = THIS_MODULE,
  217. .acpi_match_table = ACPI_PTR(int3400_thermal_match),
  218. },
  219. };
  220. module_platform_driver(int3400_thermal_driver);
  221. MODULE_DESCRIPTION("INT3400 Thermal driver");
  222. MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
  223. MODULE_LICENSE("GPL");