fan.c 5.7 KB

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