int3400_thermal.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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_ACTIVE,
  20. INT3400_THERMAL_CRITICAL,
  21. INT3400_THERMAL_ADAPTIVE_PERFORMANCE,
  22. INT3400_THERMAL_EMERGENCY_CALL_MODE,
  23. INT3400_THERMAL_PASSIVE_2,
  24. INT3400_THERMAL_POWER_BOSS,
  25. INT3400_THERMAL_VIRTUAL_SENSOR,
  26. INT3400_THERMAL_COOLING_MODE,
  27. INT3400_THERMAL_HARDWARE_DUTY_CYCLING,
  28. INT3400_THERMAL_MAXIMUM_UUID,
  29. };
  30. static u8 *int3400_thermal_uuids[INT3400_THERMAL_MAXIMUM_UUID] = {
  31. "42A441D6-AE6A-462b-A84B-4A8CE79027D3",
  32. "3A95C389-E4B8-4629-A526-C52C88626BAE",
  33. "97C68AE7-15FA-499c-B8C9-5DA81D606E0A",
  34. "63BE270F-1C11-48FD-A6F7-3AF253FF3E2D",
  35. "5349962F-71E6-431D-9AE8-0A635B710AEE",
  36. "9E04115A-AE87-4D1C-9500-0F3E340BFE75",
  37. "F5A35014-C209-46A4-993A-EB56DE7530A1",
  38. "6ED722A7-9240-48A5-B479-31EEF723D7CF",
  39. "16CAF1B7-DD38-40ED-B1C1-1B8A1913D531",
  40. "BE84BABF-C4D4-403D-B495-3128FD44dAC1",
  41. };
  42. struct int3400_thermal_priv {
  43. struct acpi_device *adev;
  44. struct thermal_zone_device *thermal;
  45. int mode;
  46. int art_count;
  47. struct art *arts;
  48. int trt_count;
  49. struct trt *trts;
  50. u8 uuid_bitmap;
  51. int rel_misc_dev_res;
  52. int current_uuid_index;
  53. };
  54. static ssize_t available_uuids_show(struct device *dev,
  55. struct device_attribute *attr,
  56. char *buf)
  57. {
  58. struct platform_device *pdev = to_platform_device(dev);
  59. struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
  60. int i;
  61. int length = 0;
  62. for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; i++) {
  63. if (priv->uuid_bitmap & (1 << i))
  64. if (PAGE_SIZE - length > 0)
  65. length += snprintf(&buf[length],
  66. PAGE_SIZE - length,
  67. "%s\n",
  68. int3400_thermal_uuids[i]);
  69. }
  70. return length;
  71. }
  72. static ssize_t current_uuid_show(struct device *dev,
  73. struct device_attribute *devattr, char *buf)
  74. {
  75. struct platform_device *pdev = to_platform_device(dev);
  76. struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
  77. if (priv->uuid_bitmap & (1 << priv->current_uuid_index))
  78. return sprintf(buf, "%s\n",
  79. int3400_thermal_uuids[priv->current_uuid_index]);
  80. else
  81. return sprintf(buf, "INVALID\n");
  82. }
  83. static ssize_t current_uuid_store(struct device *dev,
  84. struct device_attribute *attr,
  85. const char *buf, size_t count)
  86. {
  87. struct platform_device *pdev = to_platform_device(dev);
  88. struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
  89. int i;
  90. for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; ++i) {
  91. if ((priv->uuid_bitmap & (1 << i)) &&
  92. !(strncmp(buf, int3400_thermal_uuids[i],
  93. sizeof(int3400_thermal_uuids[i]) - 1))) {
  94. priv->current_uuid_index = i;
  95. return count;
  96. }
  97. }
  98. return -EINVAL;
  99. }
  100. static DEVICE_ATTR(current_uuid, 0644, current_uuid_show, current_uuid_store);
  101. static DEVICE_ATTR_RO(available_uuids);
  102. static struct attribute *uuid_attrs[] = {
  103. &dev_attr_available_uuids.attr,
  104. &dev_attr_current_uuid.attr,
  105. NULL
  106. };
  107. static struct attribute_group uuid_attribute_group = {
  108. .attrs = uuid_attrs,
  109. .name = "uuids"
  110. };
  111. static int int3400_thermal_get_uuids(struct int3400_thermal_priv *priv)
  112. {
  113. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL};
  114. union acpi_object *obja, *objb;
  115. int i, j;
  116. int result = 0;
  117. acpi_status status;
  118. status = acpi_evaluate_object(priv->adev->handle, "IDSP", NULL, &buf);
  119. if (ACPI_FAILURE(status))
  120. return -ENODEV;
  121. obja = (union acpi_object *)buf.pointer;
  122. if (obja->type != ACPI_TYPE_PACKAGE) {
  123. result = -EINVAL;
  124. goto end;
  125. }
  126. for (i = 0; i < obja->package.count; i++) {
  127. objb = &obja->package.elements[i];
  128. if (objb->type != ACPI_TYPE_BUFFER) {
  129. result = -EINVAL;
  130. goto end;
  131. }
  132. /* UUID must be 16 bytes */
  133. if (objb->buffer.length != 16) {
  134. result = -EINVAL;
  135. goto end;
  136. }
  137. for (j = 0; j < INT3400_THERMAL_MAXIMUM_UUID; j++) {
  138. u8 uuid[16];
  139. acpi_str_to_uuid(int3400_thermal_uuids[j], uuid);
  140. if (!strncmp(uuid, objb->buffer.pointer, 16)) {
  141. priv->uuid_bitmap |= (1 << j);
  142. break;
  143. }
  144. }
  145. }
  146. end:
  147. kfree(buf.pointer);
  148. return result;
  149. }
  150. static int int3400_thermal_run_osc(acpi_handle handle,
  151. enum int3400_thermal_uuid uuid, bool enable)
  152. {
  153. u32 ret, buf[2];
  154. acpi_status status;
  155. int result = 0;
  156. struct acpi_osc_context context = {
  157. .uuid_str = int3400_thermal_uuids[uuid],
  158. .rev = 1,
  159. .cap.length = 8,
  160. };
  161. buf[OSC_QUERY_DWORD] = 0;
  162. buf[OSC_SUPPORT_DWORD] = enable;
  163. context.cap.pointer = buf;
  164. status = acpi_run_osc(handle, &context);
  165. if (ACPI_SUCCESS(status)) {
  166. ret = *((u32 *)(context.ret.pointer + 4));
  167. if (ret != enable)
  168. result = -EPERM;
  169. } else
  170. result = -EPERM;
  171. kfree(context.ret.pointer);
  172. return result;
  173. }
  174. static int int3400_thermal_get_temp(struct thermal_zone_device *thermal,
  175. int *temp)
  176. {
  177. *temp = 20 * 1000; /* faked temp sensor with 20C */
  178. return 0;
  179. }
  180. static int int3400_thermal_get_mode(struct thermal_zone_device *thermal,
  181. enum thermal_device_mode *mode)
  182. {
  183. struct int3400_thermal_priv *priv = thermal->devdata;
  184. if (!priv)
  185. return -EINVAL;
  186. *mode = priv->mode;
  187. return 0;
  188. }
  189. static int int3400_thermal_set_mode(struct thermal_zone_device *thermal,
  190. enum thermal_device_mode mode)
  191. {
  192. struct int3400_thermal_priv *priv = thermal->devdata;
  193. bool enable;
  194. int result = 0;
  195. if (!priv)
  196. return -EINVAL;
  197. if (mode == THERMAL_DEVICE_ENABLED)
  198. enable = true;
  199. else if (mode == THERMAL_DEVICE_DISABLED)
  200. enable = false;
  201. else
  202. return -EINVAL;
  203. if (enable != priv->mode) {
  204. priv->mode = enable;
  205. result = int3400_thermal_run_osc(priv->adev->handle,
  206. priv->current_uuid_index,
  207. enable);
  208. }
  209. return result;
  210. }
  211. static struct thermal_zone_device_ops int3400_thermal_ops = {
  212. .get_temp = int3400_thermal_get_temp,
  213. };
  214. static struct thermal_zone_params int3400_thermal_params = {
  215. .governor_name = "user_space",
  216. .no_hwmon = true,
  217. };
  218. static int int3400_thermal_probe(struct platform_device *pdev)
  219. {
  220. struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
  221. struct int3400_thermal_priv *priv;
  222. int result;
  223. if (!adev)
  224. return -ENODEV;
  225. priv = kzalloc(sizeof(struct int3400_thermal_priv), GFP_KERNEL);
  226. if (!priv)
  227. return -ENOMEM;
  228. priv->adev = adev;
  229. result = int3400_thermal_get_uuids(priv);
  230. if (result)
  231. goto free_priv;
  232. result = acpi_parse_art(priv->adev->handle, &priv->art_count,
  233. &priv->arts, true);
  234. if (result)
  235. dev_dbg(&pdev->dev, "_ART table parsing error\n");
  236. result = acpi_parse_trt(priv->adev->handle, &priv->trt_count,
  237. &priv->trts, true);
  238. if (result)
  239. dev_dbg(&pdev->dev, "_TRT table parsing error\n");
  240. platform_set_drvdata(pdev, priv);
  241. int3400_thermal_ops.get_mode = int3400_thermal_get_mode;
  242. int3400_thermal_ops.set_mode = int3400_thermal_set_mode;
  243. priv->thermal = thermal_zone_device_register("INT3400 Thermal", 0, 0,
  244. priv, &int3400_thermal_ops,
  245. &int3400_thermal_params, 0, 0);
  246. if (IS_ERR(priv->thermal)) {
  247. result = PTR_ERR(priv->thermal);
  248. goto free_art_trt;
  249. }
  250. priv->rel_misc_dev_res = acpi_thermal_rel_misc_device_add(
  251. priv->adev->handle);
  252. result = sysfs_create_group(&pdev->dev.kobj, &uuid_attribute_group);
  253. if (result)
  254. goto free_zone;
  255. return 0;
  256. free_zone:
  257. thermal_zone_device_unregister(priv->thermal);
  258. free_art_trt:
  259. kfree(priv->trts);
  260. kfree(priv->arts);
  261. free_priv:
  262. kfree(priv);
  263. return result;
  264. }
  265. static int int3400_thermal_remove(struct platform_device *pdev)
  266. {
  267. struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
  268. if (!priv->rel_misc_dev_res)
  269. acpi_thermal_rel_misc_device_remove(priv->adev->handle);
  270. sysfs_remove_group(&pdev->dev.kobj, &uuid_attribute_group);
  271. thermal_zone_device_unregister(priv->thermal);
  272. kfree(priv->trts);
  273. kfree(priv->arts);
  274. kfree(priv);
  275. return 0;
  276. }
  277. static const struct acpi_device_id int3400_thermal_match[] = {
  278. {"INT3400", 0},
  279. {}
  280. };
  281. MODULE_DEVICE_TABLE(acpi, int3400_thermal_match);
  282. static struct platform_driver int3400_thermal_driver = {
  283. .probe = int3400_thermal_probe,
  284. .remove = int3400_thermal_remove,
  285. .driver = {
  286. .name = "int3400 thermal",
  287. .acpi_match_table = ACPI_PTR(int3400_thermal_match),
  288. },
  289. };
  290. module_platform_driver(int3400_thermal_driver);
  291. MODULE_DESCRIPTION("INT3400 Thermal driver");
  292. MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
  293. MODULE_LICENSE("GPL");