fan.c 5.4 KB

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