fan.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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
  142. || acpi_state == ACPI_STATE_D3_HOT ?
  143. 0 : (acpi_state == ACPI_STATE_D0 ? 1 : -1);
  144. return 0;
  145. }
  146. static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
  147. *state)
  148. {
  149. struct acpi_device *device = cdev->devdata;
  150. struct acpi_fan *fan = acpi_driver_data(device);
  151. if (fan->acpi4)
  152. return fan_get_state_acpi4(device, state);
  153. else
  154. return fan_get_state(device, state);
  155. }
  156. static int fan_set_state(struct acpi_device *device, unsigned long state)
  157. {
  158. if (state != 0 && state != 1)
  159. return -EINVAL;
  160. return acpi_device_set_power(device,
  161. state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
  162. }
  163. static int fan_set_state_acpi4(struct acpi_device *device, unsigned long state)
  164. {
  165. struct acpi_fan *fan = acpi_driver_data(device);
  166. acpi_status status;
  167. if (state >= fan->fps_count)
  168. return -EINVAL;
  169. status = acpi_execute_simple_method(device->handle, "_FSL",
  170. fan->fps[state].control);
  171. if (ACPI_FAILURE(status)) {
  172. dev_dbg(&device->dev, "Failed to set state by _FSL\n");
  173. return status;
  174. }
  175. return 0;
  176. }
  177. static int
  178. fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
  179. {
  180. struct acpi_device *device = cdev->devdata;
  181. struct acpi_fan *fan = acpi_driver_data(device);
  182. if (fan->acpi4)
  183. return fan_set_state_acpi4(device, state);
  184. else
  185. return fan_set_state(device, state);
  186. }
  187. static const struct thermal_cooling_device_ops fan_cooling_ops = {
  188. .get_max_state = fan_get_max_state,
  189. .get_cur_state = fan_get_cur_state,
  190. .set_cur_state = fan_set_cur_state,
  191. };
  192. /* --------------------------------------------------------------------------
  193. * Driver Interface
  194. * --------------------------------------------------------------------------
  195. */
  196. static bool acpi_fan_is_acpi4(struct acpi_device *device)
  197. {
  198. return acpi_has_method(device->handle, "_FIF") &&
  199. acpi_has_method(device->handle, "_FPS") &&
  200. acpi_has_method(device->handle, "_FSL") &&
  201. acpi_has_method(device->handle, "_FST");
  202. }
  203. static int acpi_fan_get_fif(struct acpi_device *device)
  204. {
  205. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  206. struct acpi_fan *fan = acpi_driver_data(device);
  207. struct acpi_buffer format = { sizeof("NNNN"), "NNNN" };
  208. struct acpi_buffer fif = { sizeof(fan->fif), &fan->fif };
  209. union acpi_object *obj;
  210. acpi_status status;
  211. status = acpi_evaluate_object(device->handle, "_FIF", NULL, &buffer);
  212. if (ACPI_FAILURE(status))
  213. return status;
  214. obj = buffer.pointer;
  215. if (!obj || obj->type != ACPI_TYPE_PACKAGE) {
  216. dev_err(&device->dev, "Invalid _FIF data\n");
  217. status = -EINVAL;
  218. goto err;
  219. }
  220. status = acpi_extract_package(obj, &format, &fif);
  221. if (ACPI_FAILURE(status)) {
  222. dev_err(&device->dev, "Invalid _FIF element\n");
  223. status = -EINVAL;
  224. }
  225. err:
  226. kfree(obj);
  227. return status;
  228. }
  229. static int acpi_fan_speed_cmp(const void *a, const void *b)
  230. {
  231. const struct acpi_fan_fps *fps1 = a;
  232. const struct acpi_fan_fps *fps2 = b;
  233. return fps1->speed - fps2->speed;
  234. }
  235. static int acpi_fan_get_fps(struct acpi_device *device)
  236. {
  237. struct acpi_fan *fan = acpi_driver_data(device);
  238. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  239. union acpi_object *obj;
  240. acpi_status status;
  241. int i;
  242. status = acpi_evaluate_object(device->handle, "_FPS", NULL, &buffer);
  243. if (ACPI_FAILURE(status))
  244. return status;
  245. obj = buffer.pointer;
  246. if (!obj || obj->type != ACPI_TYPE_PACKAGE || obj->package.count < 2) {
  247. dev_err(&device->dev, "Invalid _FPS data\n");
  248. status = -EINVAL;
  249. goto err;
  250. }
  251. fan->fps_count = obj->package.count - 1; /* minus revision field */
  252. fan->fps = devm_kzalloc(&device->dev,
  253. fan->fps_count * sizeof(struct acpi_fan_fps),
  254. GFP_KERNEL);
  255. if (!fan->fps) {
  256. dev_err(&device->dev, "Not enough memory\n");
  257. status = -ENOMEM;
  258. goto err;
  259. }
  260. for (i = 0; i < fan->fps_count; i++) {
  261. struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
  262. struct acpi_buffer fps = { sizeof(fan->fps[i]), &fan->fps[i] };
  263. status = acpi_extract_package(&obj->package.elements[i + 1],
  264. &format, &fps);
  265. if (ACPI_FAILURE(status)) {
  266. dev_err(&device->dev, "Invalid _FPS element\n");
  267. break;
  268. }
  269. }
  270. /* sort the state array according to fan speed in increase order */
  271. sort(fan->fps, fan->fps_count, sizeof(*fan->fps),
  272. acpi_fan_speed_cmp, NULL);
  273. err:
  274. kfree(obj);
  275. return status;
  276. }
  277. static int acpi_fan_probe(struct platform_device *pdev)
  278. {
  279. int result = 0;
  280. struct thermal_cooling_device *cdev;
  281. struct acpi_fan *fan;
  282. struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
  283. char *name;
  284. fan = devm_kzalloc(&pdev->dev, sizeof(*fan), GFP_KERNEL);
  285. if (!fan) {
  286. dev_err(&device->dev, "No memory for fan\n");
  287. return -ENOMEM;
  288. }
  289. device->driver_data = fan;
  290. platform_set_drvdata(pdev, fan);
  291. if (acpi_fan_is_acpi4(device)) {
  292. if (acpi_fan_get_fif(device) || acpi_fan_get_fps(device))
  293. goto end;
  294. fan->acpi4 = true;
  295. } else {
  296. result = acpi_device_update_power(device, NULL);
  297. if (result) {
  298. dev_err(&device->dev, "Setting initial power state\n");
  299. goto end;
  300. }
  301. }
  302. if (!strncmp(pdev->name, "PNP0C0B", strlen("PNP0C0B")))
  303. name = "Fan";
  304. else
  305. name = acpi_device_bid(device);
  306. cdev = thermal_cooling_device_register(name, device,
  307. &fan_cooling_ops);
  308. if (IS_ERR(cdev)) {
  309. result = PTR_ERR(cdev);
  310. goto end;
  311. }
  312. dev_dbg(&pdev->dev, "registered as cooling_device%d\n", cdev->id);
  313. fan->cdev = cdev;
  314. result = sysfs_create_link(&pdev->dev.kobj,
  315. &cdev->device.kobj,
  316. "thermal_cooling");
  317. if (result)
  318. dev_err(&pdev->dev, "Failed to create sysfs link 'thermal_cooling'\n");
  319. result = sysfs_create_link(&cdev->device.kobj,
  320. &pdev->dev.kobj,
  321. "device");
  322. if (result)
  323. dev_err(&pdev->dev, "Failed to create sysfs link 'device'\n");
  324. end:
  325. return result;
  326. }
  327. static int acpi_fan_remove(struct platform_device *pdev)
  328. {
  329. struct acpi_fan *fan = platform_get_drvdata(pdev);
  330. sysfs_remove_link(&pdev->dev.kobj, "thermal_cooling");
  331. sysfs_remove_link(&fan->cdev->device.kobj, "device");
  332. thermal_cooling_device_unregister(fan->cdev);
  333. return 0;
  334. }
  335. #ifdef CONFIG_PM_SLEEP
  336. static int acpi_fan_suspend(struct device *dev)
  337. {
  338. struct acpi_fan *fan = dev_get_drvdata(dev);
  339. if (fan->acpi4)
  340. return 0;
  341. acpi_device_set_power(ACPI_COMPANION(dev), ACPI_STATE_D0);
  342. return AE_OK;
  343. }
  344. static int acpi_fan_resume(struct device *dev)
  345. {
  346. int result;
  347. struct acpi_fan *fan = dev_get_drvdata(dev);
  348. if (fan->acpi4)
  349. return 0;
  350. result = acpi_device_update_power(ACPI_COMPANION(dev), NULL);
  351. if (result)
  352. dev_err(dev, "Error updating fan power state\n");
  353. return result;
  354. }
  355. #endif
  356. module_platform_driver(acpi_fan_driver);