fan.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * acpi_fan.c - ACPI Fan Driver ($Revision: 29 $)
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/thermal.h>
  31. #include <linux/acpi.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/sort.h>
  34. MODULE_AUTHOR("Paul Diefenbaugh");
  35. MODULE_DESCRIPTION("ACPI Fan Driver");
  36. MODULE_LICENSE("GPL");
  37. static int acpi_fan_probe(struct platform_device *pdev);
  38. static int acpi_fan_remove(struct platform_device *pdev);
  39. static const struct acpi_device_id fan_device_ids[] = {
  40. {"PNP0C0B", 0},
  41. {"INT3404", 0},
  42. {"", 0},
  43. };
  44. MODULE_DEVICE_TABLE(acpi, fan_device_ids);
  45. #ifdef CONFIG_PM_SLEEP
  46. static int acpi_fan_suspend(struct device *dev);
  47. static int acpi_fan_resume(struct device *dev);
  48. static struct dev_pm_ops acpi_fan_pm = {
  49. .resume = acpi_fan_resume,
  50. .freeze = acpi_fan_suspend,
  51. .thaw = acpi_fan_resume,
  52. .restore = acpi_fan_resume,
  53. };
  54. #define FAN_PM_OPS_PTR (&acpi_fan_pm)
  55. #else
  56. #define FAN_PM_OPS_PTR NULL
  57. #endif
  58. struct acpi_fan_fps {
  59. u64 control;
  60. u64 trip_point;
  61. u64 speed;
  62. u64 noise_level;
  63. u64 power;
  64. };
  65. struct acpi_fan_fif {
  66. u64 revision;
  67. u64 fine_grain_ctrl;
  68. u64 step_size;
  69. u64 low_speed_notification;
  70. };
  71. struct acpi_fan {
  72. bool acpi4;
  73. struct acpi_fan_fif fif;
  74. struct acpi_fan_fps *fps;
  75. int fps_count;
  76. struct thermal_cooling_device *cdev;
  77. };
  78. static struct platform_driver acpi_fan_driver = {
  79. .probe = acpi_fan_probe,
  80. .remove = acpi_fan_remove,
  81. .driver = {
  82. .name = "acpi-fan",
  83. .acpi_match_table = fan_device_ids,
  84. .pm = FAN_PM_OPS_PTR,
  85. },
  86. };
  87. /* thermal cooling device callbacks */
  88. static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
  89. *state)
  90. {
  91. struct acpi_device *device = cdev->devdata;
  92. struct acpi_fan *fan = acpi_driver_data(device);
  93. if (fan->acpi4)
  94. *state = fan->fps_count - 1;
  95. else
  96. *state = 1;
  97. return 0;
  98. }
  99. static int fan_get_state_acpi4(struct acpi_device *device, unsigned long *state)
  100. {
  101. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  102. struct acpi_fan *fan = acpi_driver_data(device);
  103. union acpi_object *obj;
  104. acpi_status status;
  105. int control, i;
  106. status = acpi_evaluate_object(device->handle, "_FST", NULL, &buffer);
  107. if (ACPI_FAILURE(status)) {
  108. dev_err(&device->dev, "Get fan state failed\n");
  109. return status;
  110. }
  111. obj = buffer.pointer;
  112. if (!obj || obj->type != ACPI_TYPE_PACKAGE ||
  113. obj->package.count != 3 ||
  114. obj->package.elements[1].type != ACPI_TYPE_INTEGER) {
  115. dev_err(&device->dev, "Invalid _FST data\n");
  116. status = -EINVAL;
  117. goto err;
  118. }
  119. control = obj->package.elements[1].integer.value;
  120. for (i = 0; i < fan->fps_count; i++) {
  121. if (control == fan->fps[i].control)
  122. break;
  123. }
  124. if (i == fan->fps_count) {
  125. dev_dbg(&device->dev, "Invalid control value returned\n");
  126. status = -EINVAL;
  127. goto err;
  128. }
  129. *state = i;
  130. err:
  131. kfree(obj);
  132. return status;
  133. }
  134. static int fan_get_state(struct acpi_device *device, unsigned long *state)
  135. {
  136. int result;
  137. int acpi_state = ACPI_STATE_D0;
  138. result = acpi_device_update_power(device, &acpi_state);
  139. if (result)
  140. return result;
  141. *state = (acpi_state == ACPI_STATE_D3_COLD ? 0 :
  142. (acpi_state == ACPI_STATE_D0 ? 1 : -1));
  143. return 0;
  144. }
  145. static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
  146. *state)
  147. {
  148. struct acpi_device *device = cdev->devdata;
  149. struct acpi_fan *fan = acpi_driver_data(device);
  150. if (fan->acpi4)
  151. return fan_get_state_acpi4(device, state);
  152. else
  153. return fan_get_state(device, state);
  154. }
  155. static int fan_set_state(struct acpi_device *device, unsigned long state)
  156. {
  157. if (state != 0 && state != 1)
  158. return -EINVAL;
  159. return acpi_device_set_power(device,
  160. state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
  161. }
  162. static int fan_set_state_acpi4(struct acpi_device *device, unsigned long state)
  163. {
  164. struct acpi_fan *fan = acpi_driver_data(device);
  165. acpi_status status;
  166. if (state >= fan->fps_count)
  167. return -EINVAL;
  168. status = acpi_execute_simple_method(device->handle, "_FSL",
  169. fan->fps[state].control);
  170. if (ACPI_FAILURE(status)) {
  171. dev_dbg(&device->dev, "Failed to set state by _FSL\n");
  172. return status;
  173. }
  174. return 0;
  175. }
  176. static int
  177. fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
  178. {
  179. struct acpi_device *device = cdev->devdata;
  180. struct acpi_fan *fan = acpi_driver_data(device);
  181. if (fan->acpi4)
  182. return fan_set_state_acpi4(device, state);
  183. else
  184. return fan_set_state(device, state);
  185. }
  186. static const struct thermal_cooling_device_ops fan_cooling_ops = {
  187. .get_max_state = fan_get_max_state,
  188. .get_cur_state = fan_get_cur_state,
  189. .set_cur_state = fan_set_cur_state,
  190. };
  191. /* --------------------------------------------------------------------------
  192. * Driver Interface
  193. * --------------------------------------------------------------------------
  194. */
  195. static bool acpi_fan_is_acpi4(struct acpi_device *device)
  196. {
  197. return acpi_has_method(device->handle, "_FIF") &&
  198. acpi_has_method(device->handle, "_FPS") &&
  199. acpi_has_method(device->handle, "_FSL") &&
  200. acpi_has_method(device->handle, "_FST");
  201. }
  202. static int acpi_fan_get_fif(struct acpi_device *device)
  203. {
  204. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  205. struct acpi_fan *fan = acpi_driver_data(device);
  206. struct acpi_buffer format = { sizeof("NNNN"), "NNNN" };
  207. struct acpi_buffer fif = { sizeof(fan->fif), &fan->fif };
  208. union acpi_object *obj;
  209. acpi_status status;
  210. status = acpi_evaluate_object(device->handle, "_FIF", NULL, &buffer);
  211. if (ACPI_FAILURE(status))
  212. return status;
  213. obj = buffer.pointer;
  214. if (!obj || obj->type != ACPI_TYPE_PACKAGE) {
  215. dev_err(&device->dev, "Invalid _FIF data\n");
  216. status = -EINVAL;
  217. goto err;
  218. }
  219. status = acpi_extract_package(obj, &format, &fif);
  220. if (ACPI_FAILURE(status)) {
  221. dev_err(&device->dev, "Invalid _FIF element\n");
  222. status = -EINVAL;
  223. }
  224. err:
  225. kfree(obj);
  226. return status;
  227. }
  228. static int acpi_fan_speed_cmp(const void *a, const void *b)
  229. {
  230. const struct acpi_fan_fps *fps1 = a;
  231. const struct acpi_fan_fps *fps2 = b;
  232. return fps1->speed - fps2->speed;
  233. }
  234. static int acpi_fan_get_fps(struct acpi_device *device)
  235. {
  236. struct acpi_fan *fan = acpi_driver_data(device);
  237. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  238. union acpi_object *obj;
  239. acpi_status status;
  240. int i;
  241. status = acpi_evaluate_object(device->handle, "_FPS", NULL, &buffer);
  242. if (ACPI_FAILURE(status))
  243. return status;
  244. obj = buffer.pointer;
  245. if (!obj || obj->type != ACPI_TYPE_PACKAGE || obj->package.count < 2) {
  246. dev_err(&device->dev, "Invalid _FPS data\n");
  247. status = -EINVAL;
  248. goto err;
  249. }
  250. fan->fps_count = obj->package.count - 1; /* minus revision field */
  251. fan->fps = devm_kzalloc(&device->dev,
  252. fan->fps_count * sizeof(struct acpi_fan_fps),
  253. GFP_KERNEL);
  254. if (!fan->fps) {
  255. dev_err(&device->dev, "Not enough memory\n");
  256. status = -ENOMEM;
  257. goto err;
  258. }
  259. for (i = 0; i < fan->fps_count; i++) {
  260. struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
  261. struct acpi_buffer fps = { sizeof(fan->fps[i]), &fan->fps[i] };
  262. status = acpi_extract_package(&obj->package.elements[i + 1],
  263. &format, &fps);
  264. if (ACPI_FAILURE(status)) {
  265. dev_err(&device->dev, "Invalid _FPS element\n");
  266. break;
  267. }
  268. }
  269. /* sort the state array according to fan speed in increase order */
  270. sort(fan->fps, fan->fps_count, sizeof(*fan->fps),
  271. acpi_fan_speed_cmp, NULL);
  272. err:
  273. kfree(obj);
  274. return status;
  275. }
  276. static int acpi_fan_probe(struct platform_device *pdev)
  277. {
  278. int result = 0;
  279. struct thermal_cooling_device *cdev;
  280. struct acpi_fan *fan;
  281. struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
  282. fan = devm_kzalloc(&pdev->dev, sizeof(*fan), GFP_KERNEL);
  283. if (!fan) {
  284. dev_err(&device->dev, "No memory for fan\n");
  285. return -ENOMEM;
  286. }
  287. device->driver_data = fan;
  288. platform_set_drvdata(pdev, fan);
  289. if (acpi_fan_is_acpi4(device)) {
  290. if (acpi_fan_get_fif(device) || acpi_fan_get_fps(device))
  291. goto end;
  292. fan->acpi4 = true;
  293. } else {
  294. result = acpi_device_update_power(device, NULL);
  295. if (result) {
  296. dev_err(&device->dev, "Setting initial power state\n");
  297. goto end;
  298. }
  299. }
  300. cdev = thermal_cooling_device_register("Fan", device,
  301. &fan_cooling_ops);
  302. if (IS_ERR(cdev)) {
  303. result = PTR_ERR(cdev);
  304. goto end;
  305. }
  306. dev_dbg(&pdev->dev, "registered as cooling_device%d\n", cdev->id);
  307. fan->cdev = cdev;
  308. result = sysfs_create_link(&pdev->dev.kobj,
  309. &cdev->device.kobj,
  310. "thermal_cooling");
  311. if (result)
  312. dev_err(&pdev->dev, "Failed to create sysfs link 'thermal_cooling'\n");
  313. result = sysfs_create_link(&cdev->device.kobj,
  314. &pdev->dev.kobj,
  315. "device");
  316. if (result)
  317. dev_err(&pdev->dev, "Failed to create sysfs link 'device'\n");
  318. end:
  319. return result;
  320. }
  321. static int acpi_fan_remove(struct platform_device *pdev)
  322. {
  323. struct acpi_fan *fan = platform_get_drvdata(pdev);
  324. sysfs_remove_link(&pdev->dev.kobj, "thermal_cooling");
  325. sysfs_remove_link(&fan->cdev->device.kobj, "device");
  326. thermal_cooling_device_unregister(fan->cdev);
  327. return 0;
  328. }
  329. #ifdef CONFIG_PM_SLEEP
  330. static int acpi_fan_suspend(struct device *dev)
  331. {
  332. struct acpi_fan *fan = dev_get_drvdata(dev);
  333. if (fan->acpi4)
  334. return 0;
  335. acpi_device_set_power(ACPI_COMPANION(dev), ACPI_STATE_D0);
  336. return AE_OK;
  337. }
  338. static int acpi_fan_resume(struct device *dev)
  339. {
  340. int result;
  341. struct acpi_fan *fan = dev_get_drvdata(dev);
  342. if (fan->acpi4)
  343. return 0;
  344. result = acpi_device_update_power(ACPI_COMPANION(dev), NULL);
  345. if (result)
  346. dev_err(dev, "Error updating fan power state\n");
  347. return result;
  348. }
  349. #endif
  350. module_platform_driver(acpi_fan_driver);