int3400_thermal.c 7.9 KB

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