fan.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 <asm/uaccess.h>
  30. #include <linux/thermal.h>
  31. #include <linux/acpi.h>
  32. #define PREFIX "ACPI: "
  33. #define ACPI_FAN_CLASS "fan"
  34. #define ACPI_FAN_FILE_STATE "state"
  35. #define _COMPONENT ACPI_FAN_COMPONENT
  36. ACPI_MODULE_NAME("fan");
  37. MODULE_AUTHOR("Paul Diefenbaugh");
  38. MODULE_DESCRIPTION("ACPI Fan Driver");
  39. MODULE_LICENSE("GPL");
  40. static int acpi_fan_add(struct acpi_device *device);
  41. static int acpi_fan_remove(struct acpi_device *device);
  42. static const struct acpi_device_id fan_device_ids[] = {
  43. {"PNP0C0B", 0},
  44. {"", 0},
  45. };
  46. MODULE_DEVICE_TABLE(acpi, fan_device_ids);
  47. #ifdef CONFIG_PM_SLEEP
  48. static int acpi_fan_suspend(struct device *dev);
  49. static int acpi_fan_resume(struct device *dev);
  50. static struct dev_pm_ops acpi_fan_pm = {
  51. .resume = acpi_fan_resume,
  52. .freeze = acpi_fan_suspend,
  53. .thaw = acpi_fan_resume,
  54. .restore = acpi_fan_resume,
  55. };
  56. #define FAN_PM_OPS_PTR (&acpi_fan_pm)
  57. #else
  58. #define FAN_PM_OPS_PTR NULL
  59. #endif
  60. static struct acpi_driver acpi_fan_driver = {
  61. .name = "fan",
  62. .class = ACPI_FAN_CLASS,
  63. .ids = fan_device_ids,
  64. .ops = {
  65. .add = acpi_fan_add,
  66. .remove = acpi_fan_remove,
  67. },
  68. .drv.pm = FAN_PM_OPS_PTR,
  69. };
  70. /* thermal cooling device callbacks */
  71. static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
  72. *state)
  73. {
  74. /* ACPI fan device only support two states: ON/OFF */
  75. *state = 1;
  76. return 0;
  77. }
  78. static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
  79. *state)
  80. {
  81. struct acpi_device *device = cdev->devdata;
  82. int result;
  83. int acpi_state = ACPI_STATE_D0;
  84. if (!device)
  85. return -EINVAL;
  86. result = acpi_bus_update_power(device->handle, &acpi_state);
  87. if (result)
  88. return result;
  89. *state = (acpi_state == ACPI_STATE_D3_COLD ? 0 :
  90. (acpi_state == ACPI_STATE_D0 ? 1 : -1));
  91. return 0;
  92. }
  93. static int
  94. fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
  95. {
  96. struct acpi_device *device = cdev->devdata;
  97. int result;
  98. if (!device || (state != 0 && state != 1))
  99. return -EINVAL;
  100. result = acpi_bus_set_power(device->handle,
  101. state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
  102. return result;
  103. }
  104. static const struct thermal_cooling_device_ops fan_cooling_ops = {
  105. .get_max_state = fan_get_max_state,
  106. .get_cur_state = fan_get_cur_state,
  107. .set_cur_state = fan_set_cur_state,
  108. };
  109. /* --------------------------------------------------------------------------
  110. Driver Interface
  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. printk(KERN_ERR PREFIX "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 "
  144. "'device'\n");
  145. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  146. acpi_device_name(device), acpi_device_bid(device),
  147. !device->power.state ? "on" : "off");
  148. end:
  149. return result;
  150. }
  151. static int acpi_fan_remove(struct acpi_device *device)
  152. {
  153. struct thermal_cooling_device *cdev;
  154. if (!device)
  155. return -EINVAL;
  156. cdev = acpi_driver_data(device);
  157. if (!cdev)
  158. return -EINVAL;
  159. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  160. sysfs_remove_link(&cdev->device.kobj, "device");
  161. thermal_cooling_device_unregister(cdev);
  162. return 0;
  163. }
  164. #ifdef CONFIG_PM_SLEEP
  165. static int acpi_fan_suspend(struct device *dev)
  166. {
  167. if (!dev)
  168. return -EINVAL;
  169. acpi_bus_set_power(to_acpi_device(dev)->handle, ACPI_STATE_D0);
  170. return AE_OK;
  171. }
  172. static int acpi_fan_resume(struct device *dev)
  173. {
  174. int result;
  175. if (!dev)
  176. return -EINVAL;
  177. result = acpi_bus_update_power(to_acpi_device(dev)->handle, NULL);
  178. if (result)
  179. printk(KERN_ERR PREFIX "Error updating fan power state\n");
  180. return result;
  181. }
  182. #endif
  183. module_acpi_driver(acpi_fan_driver);