fan.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #define ACPI_FAN_CLASS "fan"
  33. #define ACPI_FAN_FILE_STATE "state"
  34. #define _COMPONENT ACPI_FAN_COMPONENT
  35. ACPI_MODULE_NAME("fan");
  36. MODULE_AUTHOR("Paul Diefenbaugh");
  37. MODULE_DESCRIPTION("ACPI Fan Driver");
  38. MODULE_LICENSE("GPL");
  39. static int acpi_fan_add(struct acpi_device *device);
  40. static int acpi_fan_remove(struct acpi_device *device);
  41. static const struct acpi_device_id fan_device_ids[] = {
  42. {"PNP0C0B", 0},
  43. {"", 0},
  44. };
  45. MODULE_DEVICE_TABLE(acpi, fan_device_ids);
  46. #ifdef CONFIG_PM_SLEEP
  47. static int acpi_fan_suspend(struct device *dev);
  48. static int acpi_fan_resume(struct device *dev);
  49. static struct dev_pm_ops acpi_fan_pm = {
  50. .resume = acpi_fan_resume,
  51. .freeze = acpi_fan_suspend,
  52. .thaw = acpi_fan_resume,
  53. .restore = acpi_fan_resume,
  54. };
  55. #define FAN_PM_OPS_PTR (&acpi_fan_pm)
  56. #else
  57. #define FAN_PM_OPS_PTR NULL
  58. #endif
  59. static struct acpi_driver acpi_fan_driver = {
  60. .name = "fan",
  61. .class = ACPI_FAN_CLASS,
  62. .ids = fan_device_ids,
  63. .ops = {
  64. .add = acpi_fan_add,
  65. .remove = acpi_fan_remove,
  66. },
  67. .drv.pm = FAN_PM_OPS_PTR,
  68. };
  69. /* thermal cooling device callbacks */
  70. static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
  71. *state)
  72. {
  73. /* ACPI fan device only support two states: ON/OFF */
  74. *state = 1;
  75. return 0;
  76. }
  77. static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
  78. *state)
  79. {
  80. struct acpi_device *device = cdev->devdata;
  81. int result;
  82. int acpi_state = ACPI_STATE_D0;
  83. if (!device)
  84. return -EINVAL;
  85. result = acpi_bus_update_power(device->handle, &acpi_state);
  86. if (result)
  87. return result;
  88. *state = (acpi_state == ACPI_STATE_D3_COLD ? 0 :
  89. (acpi_state == ACPI_STATE_D0 ? 1 : -1));
  90. return 0;
  91. }
  92. static int
  93. fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
  94. {
  95. struct acpi_device *device = cdev->devdata;
  96. int result;
  97. if (!device || (state != 0 && state != 1))
  98. return -EINVAL;
  99. result = acpi_bus_set_power(device->handle,
  100. state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
  101. return result;
  102. }
  103. static const struct thermal_cooling_device_ops fan_cooling_ops = {
  104. .get_max_state = fan_get_max_state,
  105. .get_cur_state = fan_get_cur_state,
  106. .set_cur_state = fan_set_cur_state,
  107. };
  108. /* --------------------------------------------------------------------------
  109. * Driver Interface
  110. * --------------------------------------------------------------------------
  111. */
  112. static int acpi_fan_add(struct acpi_device *device)
  113. {
  114. int result = 0;
  115. struct thermal_cooling_device *cdev;
  116. if (!device)
  117. return -EINVAL;
  118. strcpy(acpi_device_name(device), "Fan");
  119. strcpy(acpi_device_class(device), ACPI_FAN_CLASS);
  120. result = acpi_bus_update_power(device->handle, NULL);
  121. if (result) {
  122. dev_err(&device->dev, "Setting initial power state\n");
  123. goto end;
  124. }
  125. cdev = thermal_cooling_device_register("Fan", device,
  126. &fan_cooling_ops);
  127. if (IS_ERR(cdev)) {
  128. result = PTR_ERR(cdev);
  129. goto end;
  130. }
  131. dev_dbg(&device->dev, "registered as cooling_device%d\n", cdev->id);
  132. device->driver_data = cdev;
  133. result = sysfs_create_link(&device->dev.kobj,
  134. &cdev->device.kobj,
  135. "thermal_cooling");
  136. if (result)
  137. dev_err(&device->dev, "Failed to create sysfs link "
  138. "'thermal_cooling'\n");
  139. result = sysfs_create_link(&cdev->device.kobj,
  140. &device->dev.kobj,
  141. "device");
  142. if (result)
  143. dev_err(&device->dev, "Failed to create sysfs link 'device'\n");
  144. dev_info(&device->dev, "ACPI: %s [%s] (%s)\n",
  145. acpi_device_name(device), acpi_device_bid(device),
  146. !device->power.state ? "on" : "off");
  147. end:
  148. return result;
  149. }
  150. static int acpi_fan_remove(struct acpi_device *device)
  151. {
  152. struct thermal_cooling_device *cdev;
  153. if (!device)
  154. return -EINVAL;
  155. cdev = acpi_driver_data(device);
  156. if (!cdev)
  157. return -EINVAL;
  158. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  159. sysfs_remove_link(&cdev->device.kobj, "device");
  160. thermal_cooling_device_unregister(cdev);
  161. return 0;
  162. }
  163. #ifdef CONFIG_PM_SLEEP
  164. static int acpi_fan_suspend(struct device *dev)
  165. {
  166. if (!dev)
  167. return -EINVAL;
  168. acpi_bus_set_power(to_acpi_device(dev)->handle, ACPI_STATE_D0);
  169. return AE_OK;
  170. }
  171. static int acpi_fan_resume(struct device *dev)
  172. {
  173. int result;
  174. if (!dev)
  175. return -EINVAL;
  176. result = acpi_bus_update_power(to_acpi_device(dev)->handle, NULL);
  177. if (result)
  178. dev_err(dev, "Error updating fan power state\n");
  179. return result;
  180. }
  181. #endif
  182. module_acpi_driver(acpi_fan_driver);