fan.c 10 KB

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