fan.c 5.5 KB

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