fan.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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_get_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. int state = 0;
  107. struct thermal_cooling_device *cdev;
  108. if (!device)
  109. return -EINVAL;
  110. strcpy(acpi_device_name(device), "Fan");
  111. strcpy(acpi_device_class(device), ACPI_FAN_CLASS);
  112. result = acpi_bus_get_power(device->handle, &state);
  113. if (result) {
  114. printk(KERN_ERR PREFIX "Reading power state\n");
  115. goto end;
  116. }
  117. device->flags.force_power_state = 1;
  118. acpi_bus_set_power(device->handle, state);
  119. device->flags.force_power_state = 0;
  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, int type)
  147. {
  148. struct thermal_cooling_device *cdev = acpi_driver_data(device);
  149. if (!device || !cdev)
  150. return -EINVAL;
  151. sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
  152. sysfs_remove_link(&cdev->device.kobj, "device");
  153. thermal_cooling_device_unregister(cdev);
  154. return 0;
  155. }
  156. static int acpi_fan_suspend(struct acpi_device *device, pm_message_t state)
  157. {
  158. if (!device)
  159. return -EINVAL;
  160. acpi_bus_set_power(device->handle, ACPI_STATE_D0);
  161. return AE_OK;
  162. }
  163. static int acpi_fan_resume(struct acpi_device *device)
  164. {
  165. int result = 0;
  166. int power_state = 0;
  167. if (!device)
  168. return -EINVAL;
  169. result = acpi_bus_get_power(device->handle, &power_state);
  170. if (result) {
  171. printk(KERN_ERR PREFIX
  172. "Error reading fan power state\n");
  173. return result;
  174. }
  175. device->flags.force_power_state = 1;
  176. acpi_bus_set_power(device->handle, power_state);
  177. device->flags.force_power_state = 0;
  178. return result;
  179. }
  180. static int __init acpi_fan_init(void)
  181. {
  182. int result = 0;
  183. result = acpi_bus_register_driver(&acpi_fan_driver);
  184. if (result < 0)
  185. return -ENODEV;
  186. return 0;
  187. }
  188. static void __exit acpi_fan_exit(void)
  189. {
  190. acpi_bus_unregister_driver(&acpi_fan_driver);
  191. return;
  192. }
  193. module_init(acpi_fan_init);
  194. module_exit(acpi_fan_exit);