int3403_thermal.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * ACPI INT3403 thermal driver
  3. * Copyright (c) 2013, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/types.h>
  18. #include <linux/acpi.h>
  19. #include <linux/thermal.h>
  20. #include <linux/platform_device.h>
  21. #include "int340x_thermal_zone.h"
  22. #define INT3403_TYPE_SENSOR 0x03
  23. #define INT3403_TYPE_CHARGER 0x0B
  24. #define INT3403_TYPE_BATTERY 0x0C
  25. #define INT3403_PERF_CHANGED_EVENT 0x80
  26. #define INT3403_PERF_TRIP_POINT_CHANGED 0x81
  27. #define INT3403_THERMAL_EVENT 0x90
  28. /* Preserved structure for future expandbility */
  29. struct int3403_sensor {
  30. struct int34x_thermal_zone *int340x_zone;
  31. };
  32. struct int3403_performance_state {
  33. u64 performance;
  34. u64 power;
  35. u64 latency;
  36. u64 linear;
  37. u64 control;
  38. u64 raw_performace;
  39. char *raw_unit;
  40. int reserved;
  41. };
  42. struct int3403_cdev {
  43. struct thermal_cooling_device *cdev;
  44. unsigned long max_state;
  45. };
  46. struct int3403_priv {
  47. struct platform_device *pdev;
  48. struct acpi_device *adev;
  49. unsigned long long type;
  50. void *priv;
  51. };
  52. static void int3403_notify(acpi_handle handle,
  53. u32 event, void *data)
  54. {
  55. struct int3403_priv *priv = data;
  56. struct int3403_sensor *obj;
  57. if (!priv)
  58. return;
  59. obj = priv->priv;
  60. if (priv->type != INT3403_TYPE_SENSOR || !obj)
  61. return;
  62. switch (event) {
  63. case INT3403_PERF_CHANGED_EVENT:
  64. break;
  65. case INT3403_THERMAL_EVENT:
  66. int340x_thermal_zone_device_update(obj->int340x_zone,
  67. THERMAL_TRIP_VIOLATED);
  68. break;
  69. case INT3403_PERF_TRIP_POINT_CHANGED:
  70. int340x_thermal_read_trips(obj->int340x_zone);
  71. int340x_thermal_zone_device_update(obj->int340x_zone,
  72. THERMAL_TRIP_CHANGED);
  73. break;
  74. default:
  75. dev_err(&priv->pdev->dev, "Unsupported event [0x%x]\n", event);
  76. break;
  77. }
  78. }
  79. static int int3403_sensor_add(struct int3403_priv *priv)
  80. {
  81. int result = 0;
  82. struct int3403_sensor *obj;
  83. obj = devm_kzalloc(&priv->pdev->dev, sizeof(*obj), GFP_KERNEL);
  84. if (!obj)
  85. return -ENOMEM;
  86. priv->priv = obj;
  87. obj->int340x_zone = int340x_thermal_zone_add(priv->adev, NULL);
  88. if (IS_ERR(obj->int340x_zone))
  89. return PTR_ERR(obj->int340x_zone);
  90. result = acpi_install_notify_handler(priv->adev->handle,
  91. ACPI_DEVICE_NOTIFY, int3403_notify,
  92. (void *)priv);
  93. if (result)
  94. goto err_free_obj;
  95. return 0;
  96. err_free_obj:
  97. int340x_thermal_zone_remove(obj->int340x_zone);
  98. return result;
  99. }
  100. static int int3403_sensor_remove(struct int3403_priv *priv)
  101. {
  102. struct int3403_sensor *obj = priv->priv;
  103. acpi_remove_notify_handler(priv->adev->handle,
  104. ACPI_DEVICE_NOTIFY, int3403_notify);
  105. int340x_thermal_zone_remove(obj->int340x_zone);
  106. return 0;
  107. }
  108. /* INT3403 Cooling devices */
  109. static int int3403_get_max_state(struct thermal_cooling_device *cdev,
  110. unsigned long *state)
  111. {
  112. struct int3403_priv *priv = cdev->devdata;
  113. struct int3403_cdev *obj = priv->priv;
  114. *state = obj->max_state;
  115. return 0;
  116. }
  117. static int int3403_get_cur_state(struct thermal_cooling_device *cdev,
  118. unsigned long *state)
  119. {
  120. struct int3403_priv *priv = cdev->devdata;
  121. unsigned long long level;
  122. acpi_status status;
  123. status = acpi_evaluate_integer(priv->adev->handle, "PPPC", NULL, &level);
  124. if (ACPI_SUCCESS(status)) {
  125. *state = level;
  126. return 0;
  127. } else
  128. return -EINVAL;
  129. }
  130. static int
  131. int3403_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
  132. {
  133. struct int3403_priv *priv = cdev->devdata;
  134. acpi_status status;
  135. status = acpi_execute_simple_method(priv->adev->handle, "SPPC", state);
  136. if (ACPI_SUCCESS(status))
  137. return 0;
  138. else
  139. return -EINVAL;
  140. }
  141. static const struct thermal_cooling_device_ops int3403_cooling_ops = {
  142. .get_max_state = int3403_get_max_state,
  143. .get_cur_state = int3403_get_cur_state,
  144. .set_cur_state = int3403_set_cur_state,
  145. };
  146. static int int3403_cdev_add(struct int3403_priv *priv)
  147. {
  148. int result = 0;
  149. acpi_status status;
  150. struct int3403_cdev *obj;
  151. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
  152. union acpi_object *p;
  153. obj = devm_kzalloc(&priv->pdev->dev, sizeof(*obj), GFP_KERNEL);
  154. if (!obj)
  155. return -ENOMEM;
  156. status = acpi_evaluate_object(priv->adev->handle, "PPSS", NULL, &buf);
  157. if (ACPI_FAILURE(status))
  158. return -ENODEV;
  159. p = buf.pointer;
  160. if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
  161. printk(KERN_WARNING "Invalid PPSS data\n");
  162. kfree(buf.pointer);
  163. return -EFAULT;
  164. }
  165. priv->priv = obj;
  166. obj->max_state = p->package.count - 1;
  167. obj->cdev =
  168. thermal_cooling_device_register(acpi_device_bid(priv->adev),
  169. priv, &int3403_cooling_ops);
  170. if (IS_ERR(obj->cdev))
  171. result = PTR_ERR(obj->cdev);
  172. kfree(buf.pointer);
  173. /* TODO: add ACPI notification support */
  174. return result;
  175. }
  176. static int int3403_cdev_remove(struct int3403_priv *priv)
  177. {
  178. struct int3403_cdev *obj = priv->priv;
  179. thermal_cooling_device_unregister(obj->cdev);
  180. return 0;
  181. }
  182. static int int3403_add(struct platform_device *pdev)
  183. {
  184. struct int3403_priv *priv;
  185. int result = 0;
  186. acpi_status status;
  187. priv = devm_kzalloc(&pdev->dev, sizeof(struct int3403_priv),
  188. GFP_KERNEL);
  189. if (!priv)
  190. return -ENOMEM;
  191. priv->pdev = pdev;
  192. priv->adev = ACPI_COMPANION(&(pdev->dev));
  193. if (!priv->adev) {
  194. result = -EINVAL;
  195. goto err;
  196. }
  197. status = acpi_evaluate_integer(priv->adev->handle, "PTYP",
  198. NULL, &priv->type);
  199. if (ACPI_FAILURE(status)) {
  200. unsigned long long tmp;
  201. status = acpi_evaluate_integer(priv->adev->handle, "_TMP",
  202. NULL, &tmp);
  203. if (ACPI_FAILURE(status)) {
  204. result = -EINVAL;
  205. goto err;
  206. } else {
  207. priv->type = INT3403_TYPE_SENSOR;
  208. }
  209. }
  210. platform_set_drvdata(pdev, priv);
  211. switch (priv->type) {
  212. case INT3403_TYPE_SENSOR:
  213. result = int3403_sensor_add(priv);
  214. break;
  215. case INT3403_TYPE_CHARGER:
  216. case INT3403_TYPE_BATTERY:
  217. result = int3403_cdev_add(priv);
  218. break;
  219. default:
  220. result = -EINVAL;
  221. }
  222. if (result)
  223. goto err;
  224. return result;
  225. err:
  226. return result;
  227. }
  228. static int int3403_remove(struct platform_device *pdev)
  229. {
  230. struct int3403_priv *priv = platform_get_drvdata(pdev);
  231. switch (priv->type) {
  232. case INT3403_TYPE_SENSOR:
  233. int3403_sensor_remove(priv);
  234. break;
  235. case INT3403_TYPE_CHARGER:
  236. case INT3403_TYPE_BATTERY:
  237. int3403_cdev_remove(priv);
  238. break;
  239. default:
  240. break;
  241. }
  242. return 0;
  243. }
  244. static const struct acpi_device_id int3403_device_ids[] = {
  245. {"INT3403", 0},
  246. {"", 0},
  247. };
  248. MODULE_DEVICE_TABLE(acpi, int3403_device_ids);
  249. static struct platform_driver int3403_driver = {
  250. .probe = int3403_add,
  251. .remove = int3403_remove,
  252. .driver = {
  253. .name = "int3403 thermal",
  254. .acpi_match_table = int3403_device_ids,
  255. },
  256. };
  257. module_platform_driver(int3403_driver);
  258. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  259. MODULE_LICENSE("GPL v2");
  260. MODULE_DESCRIPTION("ACPI INT3403 thermal driver");