int3403_thermal.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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_THERMAL_EVENT 0x90
  27. /* Preserved structure for future expandbility */
  28. struct int3403_sensor {
  29. struct int34x_thermal_zone *int340x_zone;
  30. };
  31. struct int3403_performance_state {
  32. u64 performance;
  33. u64 power;
  34. u64 latency;
  35. u64 linear;
  36. u64 control;
  37. u64 raw_performace;
  38. char *raw_unit;
  39. int reserved;
  40. };
  41. struct int3403_cdev {
  42. struct thermal_cooling_device *cdev;
  43. unsigned long max_state;
  44. };
  45. struct int3403_priv {
  46. struct platform_device *pdev;
  47. struct acpi_device *adev;
  48. unsigned long long type;
  49. void *priv;
  50. };
  51. static void int3403_notify(acpi_handle handle,
  52. u32 event, void *data)
  53. {
  54. struct int3403_priv *priv = data;
  55. struct int3403_sensor *obj;
  56. if (!priv)
  57. return;
  58. obj = priv->priv;
  59. if (priv->type != INT3403_TYPE_SENSOR || !obj)
  60. return;
  61. switch (event) {
  62. case INT3403_PERF_CHANGED_EVENT:
  63. break;
  64. case INT3403_THERMAL_EVENT:
  65. int340x_thermal_zone_device_update(obj->int340x_zone);
  66. break;
  67. default:
  68. dev_err(&priv->pdev->dev, "Unsupported event [0x%x]\n", event);
  69. break;
  70. }
  71. }
  72. static int int3403_sensor_add(struct int3403_priv *priv)
  73. {
  74. int result = 0;
  75. struct int3403_sensor *obj;
  76. obj = devm_kzalloc(&priv->pdev->dev, sizeof(*obj), GFP_KERNEL);
  77. if (!obj)
  78. return -ENOMEM;
  79. priv->priv = obj;
  80. obj->int340x_zone = int340x_thermal_zone_add(priv->adev, NULL);
  81. if (IS_ERR(obj->int340x_zone))
  82. return PTR_ERR(obj->int340x_zone);
  83. result = acpi_install_notify_handler(priv->adev->handle,
  84. ACPI_DEVICE_NOTIFY, int3403_notify,
  85. (void *)priv);
  86. if (result)
  87. goto err_free_obj;
  88. return 0;
  89. err_free_obj:
  90. int340x_thermal_zone_remove(obj->int340x_zone);
  91. return result;
  92. }
  93. static int int3403_sensor_remove(struct int3403_priv *priv)
  94. {
  95. struct int3403_sensor *obj = priv->priv;
  96. acpi_remove_notify_handler(priv->adev->handle,
  97. ACPI_DEVICE_NOTIFY, int3403_notify);
  98. int340x_thermal_zone_remove(obj->int340x_zone);
  99. return 0;
  100. }
  101. /* INT3403 Cooling devices */
  102. static int int3403_get_max_state(struct thermal_cooling_device *cdev,
  103. unsigned long *state)
  104. {
  105. struct int3403_priv *priv = cdev->devdata;
  106. struct int3403_cdev *obj = priv->priv;
  107. *state = obj->max_state;
  108. return 0;
  109. }
  110. static int int3403_get_cur_state(struct thermal_cooling_device *cdev,
  111. unsigned long *state)
  112. {
  113. struct int3403_priv *priv = cdev->devdata;
  114. unsigned long long level;
  115. acpi_status status;
  116. status = acpi_evaluate_integer(priv->adev->handle, "PPPC", NULL, &level);
  117. if (ACPI_SUCCESS(status)) {
  118. *state = level;
  119. return 0;
  120. } else
  121. return -EINVAL;
  122. }
  123. static int
  124. int3403_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
  125. {
  126. struct int3403_priv *priv = cdev->devdata;
  127. acpi_status status;
  128. status = acpi_execute_simple_method(priv->adev->handle, "SPPC", state);
  129. if (ACPI_SUCCESS(status))
  130. return 0;
  131. else
  132. return -EINVAL;
  133. }
  134. static const struct thermal_cooling_device_ops int3403_cooling_ops = {
  135. .get_max_state = int3403_get_max_state,
  136. .get_cur_state = int3403_get_cur_state,
  137. .set_cur_state = int3403_set_cur_state,
  138. };
  139. static int int3403_cdev_add(struct int3403_priv *priv)
  140. {
  141. int result = 0;
  142. acpi_status status;
  143. struct int3403_cdev *obj;
  144. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
  145. union acpi_object *p;
  146. obj = devm_kzalloc(&priv->pdev->dev, sizeof(*obj), GFP_KERNEL);
  147. if (!obj)
  148. return -ENOMEM;
  149. status = acpi_evaluate_object(priv->adev->handle, "PPSS", NULL, &buf);
  150. if (ACPI_FAILURE(status))
  151. return -ENODEV;
  152. p = buf.pointer;
  153. if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
  154. printk(KERN_WARNING "Invalid PPSS data\n");
  155. kfree(buf.pointer);
  156. return -EFAULT;
  157. }
  158. obj->max_state = p->package.count - 1;
  159. obj->cdev =
  160. thermal_cooling_device_register(acpi_device_bid(priv->adev),
  161. priv, &int3403_cooling_ops);
  162. if (IS_ERR(obj->cdev))
  163. result = PTR_ERR(obj->cdev);
  164. priv->priv = obj;
  165. kfree(buf.pointer);
  166. /* TODO: add ACPI notification support */
  167. return result;
  168. }
  169. static int int3403_cdev_remove(struct int3403_priv *priv)
  170. {
  171. struct int3403_cdev *obj = priv->priv;
  172. thermal_cooling_device_unregister(obj->cdev);
  173. return 0;
  174. }
  175. static int int3403_add(struct platform_device *pdev)
  176. {
  177. struct int3403_priv *priv;
  178. int result = 0;
  179. acpi_status status;
  180. priv = devm_kzalloc(&pdev->dev, sizeof(struct int3403_priv),
  181. GFP_KERNEL);
  182. if (!priv)
  183. return -ENOMEM;
  184. priv->pdev = pdev;
  185. priv->adev = ACPI_COMPANION(&(pdev->dev));
  186. if (!priv->adev) {
  187. result = -EINVAL;
  188. goto err;
  189. }
  190. status = acpi_evaluate_integer(priv->adev->handle, "PTYP",
  191. NULL, &priv->type);
  192. if (ACPI_FAILURE(status)) {
  193. result = -EINVAL;
  194. goto err;
  195. }
  196. platform_set_drvdata(pdev, priv);
  197. switch (priv->type) {
  198. case INT3403_TYPE_SENSOR:
  199. result = int3403_sensor_add(priv);
  200. break;
  201. case INT3403_TYPE_CHARGER:
  202. case INT3403_TYPE_BATTERY:
  203. result = int3403_cdev_add(priv);
  204. break;
  205. default:
  206. result = -EINVAL;
  207. }
  208. if (result)
  209. goto err;
  210. return result;
  211. err:
  212. return result;
  213. }
  214. static int int3403_remove(struct platform_device *pdev)
  215. {
  216. struct int3403_priv *priv = platform_get_drvdata(pdev);
  217. switch (priv->type) {
  218. case INT3403_TYPE_SENSOR:
  219. int3403_sensor_remove(priv);
  220. break;
  221. case INT3403_TYPE_CHARGER:
  222. case INT3403_TYPE_BATTERY:
  223. int3403_cdev_remove(priv);
  224. break;
  225. default:
  226. break;
  227. }
  228. return 0;
  229. }
  230. static const struct acpi_device_id int3403_device_ids[] = {
  231. {"INT3403", 0},
  232. {"", 0},
  233. };
  234. MODULE_DEVICE_TABLE(acpi, int3403_device_ids);
  235. static struct platform_driver int3403_driver = {
  236. .probe = int3403_add,
  237. .remove = int3403_remove,
  238. .driver = {
  239. .name = "int3403 thermal",
  240. .acpi_match_table = int3403_device_ids,
  241. },
  242. };
  243. module_platform_driver(int3403_driver);
  244. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  245. MODULE_LICENSE("GPL v2");
  246. MODULE_DESCRIPTION("ACPI INT3403 thermal driver");