fan.c 11 KB

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