int3400_thermal.c 9.5 KB

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