fan.c 5.4 KB

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