fan.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * acpi_fan.c - ACPI Fan Driver ($Revision: 29 $)
  4. *
  5. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/types.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/thermal.h>
  14. #include <linux/acpi.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/sort.h>
  17. #include "fan.h"
  18. MODULE_AUTHOR("Paul Diefenbaugh");
  19. MODULE_DESCRIPTION("ACPI Fan Driver");
  20. MODULE_LICENSE("GPL");
  21. static int acpi_fan_probe(struct platform_device *pdev);
  22. static int acpi_fan_remove(struct platform_device *pdev);
  23. static const struct acpi_device_id fan_device_ids[] = {
  24. ACPI_FAN_DEVICE_IDS,
  25. {"", 0},
  26. };
  27. MODULE_DEVICE_TABLE(acpi, fan_device_ids);
  28. #ifdef CONFIG_PM_SLEEP
  29. static int acpi_fan_suspend(struct device *dev);
  30. static int acpi_fan_resume(struct device *dev);
  31. static const struct dev_pm_ops acpi_fan_pm = {
  32. .resume = acpi_fan_resume,
  33. .freeze = acpi_fan_suspend,
  34. .thaw = acpi_fan_resume,
  35. .restore = acpi_fan_resume,
  36. };
  37. #define FAN_PM_OPS_PTR (&acpi_fan_pm)
  38. #else
  39. #define FAN_PM_OPS_PTR NULL
  40. #endif
  41. #define ACPI_FPS_NAME_LEN 20
  42. struct acpi_fan_fps {
  43. u64 control;
  44. u64 trip_point;
  45. u64 speed;
  46. u64 noise_level;
  47. u64 power;
  48. char name[ACPI_FPS_NAME_LEN];
  49. struct device_attribute dev_attr;
  50. };
  51. struct acpi_fan_fif {
  52. u64 revision;
  53. u64 fine_grain_ctrl;
  54. u64 step_size;
  55. u64 low_speed_notification;
  56. };
  57. struct acpi_fan {
  58. bool acpi4;
  59. struct acpi_fan_fif fif;
  60. struct acpi_fan_fps *fps;
  61. int fps_count;
  62. struct thermal_cooling_device *cdev;
  63. };
  64. static struct platform_driver acpi_fan_driver = {
  65. .probe = acpi_fan_probe,
  66. .remove = acpi_fan_remove,
  67. .driver = {
  68. .name = "acpi-fan",
  69. .acpi_match_table = fan_device_ids,
  70. .pm = FAN_PM_OPS_PTR,
  71. },
  72. };
  73. /* thermal cooling device callbacks */
  74. static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
  75. *state)
  76. {
  77. struct acpi_device *device = cdev->devdata;
  78. struct acpi_fan *fan = acpi_driver_data(device);
  79. if (fan->acpi4)
  80. *state = fan->fps_count - 1;
  81. else
  82. *state = 1;
  83. return 0;
  84. }
  85. static int fan_get_state_acpi4(struct acpi_device *device, unsigned long *state)
  86. {
  87. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  88. struct acpi_fan *fan = acpi_driver_data(device);
  89. union acpi_object *obj;
  90. acpi_status status;
  91. int control, i;
  92. status = acpi_evaluate_object(device->handle, "_FST", NULL, &buffer);
  93. if (ACPI_FAILURE(status)) {
  94. dev_err(&device->dev, "Get fan state failed\n");
  95. return status;
  96. }
  97. obj = buffer.pointer;
  98. if (!obj || obj->type != ACPI_TYPE_PACKAGE ||
  99. obj->package.count != 3 ||
  100. obj->package.elements[1].type != ACPI_TYPE_INTEGER) {
  101. dev_err(&device->dev, "Invalid _FST data\n");
  102. status = -EINVAL;
  103. goto err;
  104. }
  105. control = obj->package.elements[1].integer.value;
  106. for (i = 0; i < fan->fps_count; i++) {
  107. /*
  108. * When Fine Grain Control is set, return the state
  109. * corresponding to maximum fan->fps[i].control
  110. * value compared to the current speed. Here the
  111. * fan->fps[] is sorted array with increasing speed.
  112. */
  113. if (fan->fif.fine_grain_ctrl && control < fan->fps[i].control) {
  114. i = (i > 0) ? i - 1 : 0;
  115. break;
  116. } else if (control == fan->fps[i].control) {
  117. break;
  118. }
  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 ssize_t show_state(struct device *dev, struct device_attribute *attr, char *buf)
  232. {
  233. struct acpi_fan_fps *fps = container_of(attr, struct acpi_fan_fps, dev_attr);
  234. int count;
  235. if (fps->control == 0xFFFFFFFF || fps->control > 100)
  236. count = scnprintf(buf, PAGE_SIZE, "not-defined:");
  237. else
  238. count = scnprintf(buf, PAGE_SIZE, "%lld:", fps->control);
  239. if (fps->trip_point == 0xFFFFFFFF || fps->trip_point > 9)
  240. count += scnprintf(&buf[count], PAGE_SIZE - count, "not-defined:");
  241. else
  242. count += scnprintf(&buf[count], PAGE_SIZE - count, "%lld:", fps->trip_point);
  243. if (fps->speed == 0xFFFFFFFF)
  244. count += scnprintf(&buf[count], PAGE_SIZE - count, "not-defined:");
  245. else
  246. count += scnprintf(&buf[count], PAGE_SIZE - count, "%lld:", fps->speed);
  247. if (fps->noise_level == 0xFFFFFFFF)
  248. count += scnprintf(&buf[count], PAGE_SIZE - count, "not-defined:");
  249. else
  250. count += scnprintf(&buf[count], PAGE_SIZE - count, "%lld:", fps->noise_level * 100);
  251. if (fps->power == 0xFFFFFFFF)
  252. count += scnprintf(&buf[count], PAGE_SIZE - count, "not-defined\n");
  253. else
  254. count += scnprintf(&buf[count], PAGE_SIZE - count, "%lld\n", fps->power);
  255. return count;
  256. }
  257. static int acpi_fan_get_fps(struct acpi_device *device)
  258. {
  259. struct acpi_fan *fan = acpi_driver_data(device);
  260. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  261. union acpi_object *obj;
  262. acpi_status status;
  263. int i;
  264. status = acpi_evaluate_object(device->handle, "_FPS", NULL, &buffer);
  265. if (ACPI_FAILURE(status))
  266. return status;
  267. obj = buffer.pointer;
  268. if (!obj || obj->type != ACPI_TYPE_PACKAGE || obj->package.count < 2) {
  269. dev_err(&device->dev, "Invalid _FPS data\n");
  270. status = -EINVAL;
  271. goto err;
  272. }
  273. fan->fps_count = obj->package.count - 1; /* minus revision field */
  274. fan->fps = devm_kcalloc(&device->dev,
  275. fan->fps_count, sizeof(struct acpi_fan_fps),
  276. GFP_KERNEL);
  277. if (!fan->fps) {
  278. dev_err(&device->dev, "Not enough memory\n");
  279. status = -ENOMEM;
  280. goto err;
  281. }
  282. for (i = 0; i < fan->fps_count; i++) {
  283. struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
  284. struct acpi_buffer fps = { offsetof(struct acpi_fan_fps, name),
  285. &fan->fps[i] };
  286. status = acpi_extract_package(&obj->package.elements[i + 1],
  287. &format, &fps);
  288. if (ACPI_FAILURE(status)) {
  289. dev_err(&device->dev, "Invalid _FPS element\n");
  290. goto err;
  291. }
  292. }
  293. /* sort the state array according to fan speed in increase order */
  294. sort(fan->fps, fan->fps_count, sizeof(*fan->fps),
  295. acpi_fan_speed_cmp, NULL);
  296. for (i = 0; i < fan->fps_count; ++i) {
  297. struct acpi_fan_fps *fps = &fan->fps[i];
  298. snprintf(fps->name, ACPI_FPS_NAME_LEN, "state%d", i);
  299. sysfs_attr_init(&fps->dev_attr.attr);
  300. fps->dev_attr.show = show_state;
  301. fps->dev_attr.store = NULL;
  302. fps->dev_attr.attr.name = fps->name;
  303. fps->dev_attr.attr.mode = 0444;
  304. status = sysfs_create_file(&device->dev.kobj, &fps->dev_attr.attr);
  305. if (status) {
  306. int j;
  307. for (j = 0; j < i; ++j)
  308. sysfs_remove_file(&device->dev.kobj, &fan->fps[j].dev_attr.attr);
  309. break;
  310. }
  311. }
  312. err:
  313. kfree(obj);
  314. return status;
  315. }
  316. static int acpi_fan_probe(struct platform_device *pdev)
  317. {
  318. int result = 0;
  319. struct thermal_cooling_device *cdev;
  320. struct acpi_fan *fan;
  321. struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
  322. char *name;
  323. fan = devm_kzalloc(&pdev->dev, sizeof(*fan), GFP_KERNEL);
  324. if (!fan) {
  325. dev_err(&device->dev, "No memory for fan\n");
  326. return -ENOMEM;
  327. }
  328. device->driver_data = fan;
  329. platform_set_drvdata(pdev, fan);
  330. if (acpi_fan_is_acpi4(device)) {
  331. result = acpi_fan_get_fif(device);
  332. if (result)
  333. return result;
  334. result = acpi_fan_get_fps(device);
  335. if (result)
  336. return result;
  337. fan->acpi4 = true;
  338. } else {
  339. result = acpi_device_update_power(device, NULL);
  340. if (result) {
  341. dev_err(&device->dev, "Failed to set initial power state\n");
  342. goto err_end;
  343. }
  344. }
  345. if (!strncmp(pdev->name, "PNP0C0B", strlen("PNP0C0B")))
  346. name = "Fan";
  347. else
  348. name = acpi_device_bid(device);
  349. cdev = thermal_cooling_device_register(name, device,
  350. &fan_cooling_ops);
  351. if (IS_ERR(cdev)) {
  352. result = PTR_ERR(cdev);
  353. goto err_end;
  354. }
  355. dev_dbg(&pdev->dev, "registered as cooling_device%d\n", cdev->id);
  356. fan->cdev = cdev;
  357. result = sysfs_create_link(&pdev->dev.kobj,
  358. &cdev->device.kobj,
  359. "thermal_cooling");
  360. if (result)
  361. dev_err(&pdev->dev, "Failed to create sysfs link 'thermal_cooling'\n");
  362. result = sysfs_create_link(&cdev->device.kobj,
  363. &pdev->dev.kobj,
  364. "device");
  365. if (result) {
  366. dev_err(&pdev->dev, "Failed to create sysfs link 'device'\n");
  367. goto err_end;
  368. }
  369. return 0;
  370. err_end:
  371. if (fan->acpi4) {
  372. int i;
  373. for (i = 0; i < fan->fps_count; ++i)
  374. sysfs_remove_file(&device->dev.kobj, &fan->fps[i].dev_attr.attr);
  375. }
  376. return result;
  377. }
  378. static int acpi_fan_remove(struct platform_device *pdev)
  379. {
  380. struct acpi_fan *fan = platform_get_drvdata(pdev);
  381. if (fan->acpi4) {
  382. struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
  383. int i;
  384. for (i = 0; i < fan->fps_count; ++i)
  385. sysfs_remove_file(&device->dev.kobj, &fan->fps[i].dev_attr.attr);
  386. }
  387. sysfs_remove_link(&pdev->dev.kobj, "thermal_cooling");
  388. sysfs_remove_link(&fan->cdev->device.kobj, "device");
  389. thermal_cooling_device_unregister(fan->cdev);
  390. return 0;
  391. }
  392. #ifdef CONFIG_PM_SLEEP
  393. static int acpi_fan_suspend(struct device *dev)
  394. {
  395. struct acpi_fan *fan = dev_get_drvdata(dev);
  396. if (fan->acpi4)
  397. return 0;
  398. acpi_device_set_power(ACPI_COMPANION(dev), ACPI_STATE_D0);
  399. return AE_OK;
  400. }
  401. static int acpi_fan_resume(struct device *dev)
  402. {
  403. int result;
  404. struct acpi_fan *fan = dev_get_drvdata(dev);
  405. if (fan->acpi4)
  406. return 0;
  407. result = acpi_device_update_power(ACPI_COMPANION(dev), NULL);
  408. if (result)
  409. dev_err(dev, "Error updating fan power state\n");
  410. return result;
  411. }
  412. #endif
  413. module_platform_driver(acpi_fan_driver);