fan.c 5.5 KB

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