fan_core.c 11 KB

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