int3400_thermal.c 9.0 KB

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